CSV Dicteader Doesn’t Have To Be Hard

Hello geeks, So welcome back to another article from us. So, in this article, we will learn about the “CsvDictReader” module. We will see how we can read CSV files in python using the CSV module and different functionalities available for manipulating data and operations on CSV data. So, Let’s get started.

CSV Module

So, before heading toward DictReader class, let’s take a quick overview of the CSV module. So, CSVs, also known as Comma Separated Values, is one of the most used formats for representing, importing, and exporting tabular data. This data may be of excel sheets or any database. This data format is generally used between the programs where they aren’t ordinally able to exchange data.

Now, while exchanging data, we need some programming interfaces that provide us the functionality of interaction with those files. Here, comes in the picture, CSV module. This module is inbuilt in python libraries and hence requires no installation. We need to import it while handling CSV files. To do that, we will use the following commands.

import csv

This module has several classes and functions that provide different access for reading & writing data and making changes in the files. Some of the classes are CSV.reader, CSV.writer, CSV.DictReader, CSV.DictWriter, CSV.excel e.t.c.. Although each class has its use, we will only focus on CSV in this article.DictReader class.

CSV.DictReader

So, CSV.Dictreader creates a dictionary object for each row of the CSV file and maps each row value to the column name as the key. Keys and Values are stored as the string. So before heading toward examples, let’s see the syntax for the same.

Syntax:

csv.DictReader(f, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)

Now, once you get the same, let’s take a clear example to see its use and discuss each argument for the same.

airtravel.csv

"Month", "1958", "1959", "1960"
"JAN",  340,  360,  417
"FEB",  318,  342,  391
"MAR",  362,  406,  419
"APR",  348,  396,  461
"MAY",  363,  420,  472
"JUN",  435,  472,  535
"JUL",  491,  548,  622
"AUG",  505,  559,  606
"SEP",  404,  463,  508
"OCT",  359,  407,  461
"NOV",  310,  362,  390
"DEC",  337,  405,  432

Example 1:

# importing DictReader class  from csv module
from csv import DictReader

# opening csv file named as airtravel.csv
with open('airtravel.csv','r') as file:
    reader = DictReader(file)
    print("Data:")
    # printing each row of table as dictionary 
    for row in reader:
        print(row)

Output:

Data:
{'Month': 'JAN', ' "1958"': '  340', ' "1959"': '  360', ' "1960"': '  417'}
{'Month': 'FEB', ' "1958"': '  318', ' "1959"': '  342', ' "1960"': '  391'}
{'Month': 'MAR', ' "1958"': '  362', ' "1959"': '  406', ' "1960"': '  419'}
{'Month': 'APR', ' "1958"': '  348', ' "1959"': '  396', ' "1960"': '  461'}
{'Month': 'MAY', ' "1958"': '  363', ' "1959"': '  420', ' "1960"': '  472'}
{'Month': 'JUN', ' "1958"': '  435', ' "1959"': '  472', ' "1960"': '  535'}
{'Month': 'JUL', ' "1958"': '  491', ' "1959"': '  548', ' "1960"': '  622'}
{'Month': 'AUG', ' "1958"': '  505', ' "1959"': '  559', ' "1960"': '  606'}
{'Month': 'SEP', ' "1958"': '  404', ' "1959"': '  463', ' "1960"': '  508'}
{'Month': 'OCT', ' "1958"': '  359', ' "1959"': '  407', ' "1960"': '  461'}
{'Month': 'NOV', ' "1958"': '  310', ' "1959"': '  362', ' "1960"': '  390'}
{'Month': 'DEC', ' "1958"': '  337', ' "1959"': '  405', ' "1960"': '  432'}

So, in the above example, what we have done is we first imported DictReader class from the CSV module. After that, we opened the file airtarvel.csv in “read” mode, which consists of the number of monthly travelers in 1958, 1959, and 1960. Once we opened the file, we read it using Dictreader class and then printed each row of the dictionary.

Now once, you understand how we can use CSV.DictReader for reading the CSV file. Let’s understand the use of different parameters available for initializing the object.

  • f: It is the variable name of the csv file that we need to read.
  • fieldnames: This argument is used to set the key values for the dictionary object. If it is left blank, the interpreter itself interpret the first line of the file as the key values.

Let’s see an example of that. But we first omitted the first row from the file and then included a list of keys as the field names argument in our code.

Example 2: Adding column to CSV Dictreader

airtravel.csv

"JAN",  340,  360,  417
"FEB",  318,  342,  391
"MAR",  362,  406,  419
"APR",  348,  396,  461
"MAY",  363,  420,  472
"JUN",  435,  472,  535
"JUL",  491,  548,  622
"AUG",  505,  559,  606
"SEP",  404,  463,  508
"OCT",  359,  407,  461
"NOV",  310,  362,  390
"DEC",  337,  405,  432

# importing DictReader class  from csv module
from csv import DictReader

# opening csv file named as airtravel.csv
with open('airtravel.csv','r') as file:
    reader = DictReader(file,fieldnames=['Month','1958','1959','1960'])
    print("Data:")
    # printing each row of table as dictionary 
    for row in reader:
        print(row)

Output:

{'Month': 'JAN', '1958': '  340', '1959': '  360', '1960': '  417'}
{'Month': 'FEB', '1958': '  318', '1959': '  342', '1960': '  391'}
{'Month': 'MAR', '1958': '  362', '1959': '  406', '1960': '  419'}
{'Month': 'APR', '1958': '  348', '1959': '  396', '1960': '  461'}
{'Month': 'MAY', '1958': '  363', '1959': '  420', '1960': '  472'}
{'Month': 'JUN', '1958': '  435', '1959': '  472', '1960': '  535'}
{'Month': 'JUL', '1958': '  491', '1959': '  548', '1960': '  622'}
{'Month': 'AUG', '1958': '  505', '1959': '  559', '1960': '  606'}
{'Month': 'SEP', '1958': '  404', '1959': '  463', '1960': '  508'}
{'Month': 'OCT', '1958': '  359', '1959': '  407', '1960': '  461'}
{'Month': 'NOV', '1958': '  310', '1959': '  362', '1960': '  390'}
{'Month': 'DEC', '1958': '  337', '1959': '  405', '1960': '  432'}
  • restkey & restval :- If a row has more fields than fieldnames, the remaining data is put in a list and stored with the fieldname specified by restkey.

Exmaple 3:- Changing Column name in CSV DictReader

We can use the field names argument while initiating the object to change the column’s name. It will change the default values of the column in our code. However, it will not change the column name in the CSV file. Let’s see the sample code.

# importing DictReader class  from csv module
from csv import DictReader

# opening csv file named as airtravel.csv
with open('airtravel.csv','r') as file:
    reader = DictReader(file,fieldnames=['New-Month','FY-1958','FY-1959','FY-1960'])
    print("Data:")
    # printing each row of table as dictionary 
    for row in reader:
        print(row)

Example 4: Case Insensitive Dict Reader

To achieve this goal, what we can do is we can take the help of an OOPs concept called inheritance. We will create a new class that inherits DictReader class, and then we add the code for case insensitivity in that class. Once we are done with creating the class, we will call that class to instantiate the object of that class. Let’s see that.

import csv

class InsensitiveDictReader(csv.DictReader):
    # This class overrides the csv.fieldnames property, which converts all fieldnames without leading and trailing spaces and to lower case.

    @property
    def fieldnames(self):
        return [field.strip().lower() for field in csv.DictReader.fieldnames.fget(self)]

    def next(self):
        return InsensitiveDict(csv.DictReader.next(self))

class InsensitiveDict(dict):
    # This class overrides the __getitem__ method to automatically strip() and lower() the input key

    def __getitem__(self, key):
        return dict.__getitem__(self, key.strip().lower())

with open('airtravel.csv','r') as file:
    reader = InsensitiveDictReader(file,fieldnames=['Month Name','1958','1959','1960'])
    print("Data:")
    # printing each row of table as dictionary 
    for row in reader:
        print(row)

Output:

{'month name': 'JAN', '1958': '  340', '1959': '  360', '1960': '  417'}
{'month name': 'FEB', '1958': '  318', '1959': '  342', '1960': '  391'}
{'month name': 'MAR', '1958': '  362', '1959': '  406', '1960': '  419'}
{'month name': 'APR', '1958': '  348', '1959': '  396', '1960': '  461'}
{'month name': 'MAY', '1958': '  363', '1959': '  420', '1960': '  472'}
{'month name': 'JUN', '1958': '  435', '1959': '  472', '1960': '  535'}
{'month name': 'JUL', '1958': '  491', '1959': '  548', '1960': '  '}
{'month name': 'AUG', '1958': '  505', '1959': '  559', '1960': '  606'}
{'month name': 'SEP', '1958': '  404', '1959': '  463', '1960': '  508'}
{'month name': 'OCT', '1958': '  359', '1959': '  407', '1960': '  461'}
{'month name': 'NOV', '1958': '  310', '1959': '  362', '1960': '  390'}
{'month name': 'DEC', '1958': '  337', '1959': '  405', '1960': '  432'}

Example 5: Python CSV DictReader utf-8

To create a dictionary object for utf-8 encoded data, you can use the following function.

def UnicodeDictReader(utf8_data, **kwargs):
    csv_reader = csv.DictReader(utf8_data, **kwargs)
    for row in csv_reader:
        yield {unicode(key, 'utf-8'):unicode(value, 'utf-8') for key, value in row.iteritems()}

Example 6: Python CSV DictReader ignore empty lines

We need not write different code to ignore empty lines in CSV DictReader. The class themselves ignore blank lines in the CSV file while reading it.

FAQs on Python CSV Dictreader

Q1) Can we convert CSV dictreader to JSON?

Yes, we can convert our dict object into a JSON object. To do that, we will use the following line of code.

# importing DictReader class  from csv module
from csv import DictReader
import json

# opening csv file named as airtravel.csv
with open('airtravel.csv','r') as file:
    reader = DictReader(file)
    jsonfile = open('file.json', 'w')
    # printing each row of table as dictionary
    for row in reader:      
        json.dump(row,jsonfile)
        jsonfile.write('\n')

Conclusion

So, today in this article, we have learned about python CSV.DictReader class. We have seen how we can use it for reading a CSV file and then perform the operations on them.

I hope this article has helped you. Thank you.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments