Placing The Data In Respective Field Names In A Csv File Using Python
Im writing a script that extracts the data in a txt file and write in CSV file....I can able to Split the data but could not able to place it in the correct field name...... The fi
Solution 1:
The problem is that your regular expression doesn't even attempt to match the field names, it just matches everything in quotes.
If you write a regular expression that does match the field names, then you'll get the field names. For example:
re.findall(r'(\S+?)\s*=\s*\"(.+?)\"',line)
This matches a group of non-whitespace characters followed by any whitespace, an =
, any whitespace, and a group of anything in quotes. Since there are two capturing groups, each output element will be a 2-tuple, like:
('xxxx', 'qqqqqqq 5466 78455')
So you can just convert the list of 2-tuples into a dict
, and use csv.DictWriter
to match the field names with the CSV volume names.
Post a Comment for "Placing The Data In Respective Field Names In A Csv File Using Python"