1.

What is the difference between reader( ) and DictReader( ) function?

Answer»

Reading CSV File Into A Dictionary: To read a CSV file into a dictionary can be done by using DictReader class of csv module which works similar to the reader( ) class but creates an object which maps data to a dictionary. The keys are given by the fieldnames as parameter. DictReader works by reading the first line of the CSV and using each comma separated value in this line as a dictionary key.

The columns in each subsequent row then behave like dictionary values and can be accessed with the appropriate key (i.e. fieldname). The main difference between the csv.reader( ) and DictReader( ) is in simple terms csv. reader and csv.writer work with list/tuple, while csv.DictReader and csv.DictWriter work ‘ with dictionary. csv.DictReader and csv.DictWriter take additional argument fieldnames that are used as dictionary keys.



Discussion

No Comment Found