Python: 3.x
Hi. i have below csv file, which has header and rows. rows count may vary file to file. i am trying to convert this csv to a dict format and data is being repeated for first row.
"cdrRecordType","globalCallID_callManagerId","globalCallID_callId"
1,3,9294899
1,3,9294933
Code:
parserd_list = []
output_dict = {}
with open("files\\CUCMdummy.csv") as myfile:
firstline = True
for line in myfile:
if firstline:
mykeys = ''.join(line.split()).split(',')
firstline = False
else:
values = ''.join(line.split()).split(',')
for n in range(len(mykeys)):
output_dict[mykeys[n].rstrip('"').lstrip('"')] = values[n].rstrip('"').lstrip('"')
print(output_dict)
parserd_list.append(output_dict)
#print(parserd_list)
(Generally my csv column count is more than 20, but i have presented a sample file.)
(i have used rstrip/lstrip to get rid of double quotes.)
Output getting:
{'cdrRecordType': '1'}
{'cdrRecordType': '1', 'globalCallID_callManagerId': '3'}
{'cdrRecordType': '1', 'globalCallID_callManagerId': '3', 'globalCallID_callId': '9294899'}
{'cdrRecordType': '1', 'globalCallID_callManagerId': '3', 'globalCallID_callId': '9294899'}
{'cdrRecordType': '1', 'globalCallID_callManagerId': '3', 'globalCallID_callId': '9294899'}
{'cdrRecordType': '1', 'globalCallID_callManagerId': '3', 'globalCallID_callId': '9294933'}
this is the output of print inside for loop. and final output is also the same.
i dont know what mistake i am doing. Someone please help correct it.
thanks in advance.
Instead of manually parsing a CSV file, you should use the csv module.
This will result in a simpler script and will facilitate gracefully handling edge cases (e.g. header row, inconsistently quoted fields, etc.).
import csv
with open('example.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row)
Output:
$ python3 parse-csv.py
OrderedDict([('cdrRecordType', '1'), ('globalCallID_callManagerId', '3'), ('globalCallID_callId', '9294899')])
OrderedDict([('cdrRecordType', '1'), ('globalCallID_callManagerId', '3'), ('globalCallID_callId', '9294933')])
If you're intent on parsing manually, here's an approach for doing so:
parsed_list = []
with open('example.csv') as myfile:
firstline = True
for line in myfile:
# Strip leading/trailing whitespace and split into a list of values.
values = line.strip().split(',')
# Remove surrounding double quotes from each value, if they exist.
values = [v.strip('"') for v in values]
# Use the first line as keys.
if firstline:
keys = values
firstline = False
# Skip to the next iteration of the for loop.
continue
parsed_list.append(dict(zip(keys, values)))
for p in parsed_list:
print(p)
Output:
$ python3 manual-parse-csv.py
{'cdrRecordType': '1', 'globalCallID_callManagerId': '3', 'globalCallID_callId': '9294899'}
{'cdrRecordType': '1', 'globalCallID_callManagerId': '3', 'globalCallID_callId': '9294933'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With