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?
csv. Reader() allows you to access CSV data using indexes and is ideal for simple CSV files. csv. DictReader() on the other hand is friendlier and easy to use, especially when working with large CSV files.
The csv. DictReader class operates like a regular reader but maps the information read into a dictionary. The keys for the dictionary can be passed in with the fieldnames parameter or inferred from the first row of the CSV file.
DictReader() Class. The objects of a csv. DictReader() class can be used to read a CSV file as a dictionary.
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.
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