Reputation: 963
I need to check the column headers in a csv file, and only need the header values for this task, not the values below (yet). Right now I am using DictReader and fieldnames to get column headers, mainly because the code is much clearer as to what is occurring using this method. Is there any reason (performance or otherwise) to just use the standard csv reader, read in the top column as headers, and then close the file?
Upvotes: 5
Views: 8303
Reputation: 55942
I would imagine that the DictReader would be only slightly less efficient than the csv reader (I have never seen benchmarks though). What the DictReader does offer is better readability. Anyone reading your code will be able to see what is going on when you access a column
reader = csv.DictReader(f.open)
for line_dict in reader:
line_dict['First Column']
line_dict['Second Column']
opposed to just numerical indexes of the csv reader. If you are working with files with many columns, having to remember indexes to column names can be maddening. I think for readability the DictReader wins.
Upvotes: 8