Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DictReader fieldnames vs csv reader first row

Tags:

python

csv

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?

like image 274
Sam Johnson Avatar asked Mar 26 '12 17:03

Sam Johnson


People also ask

What is the difference between csv reader and csv DictReader?

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.

What does csv DictReader do?

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.

What function reads in a CSV file as a Python dictionary assuming the first row of the csv contains headers?

DictReader() Class. The objects of a csv. DictReader() class can be used to read a CSV file as a dictionary.


1 Answers

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.

like image 173
dm03514 Avatar answered Sep 29 '22 05:09

dm03514