Menu Close

Read CSV Files in Python

Read CSV files in Python

In this article, you will learn all about How to read CSV files in Python with the help of examples. Here we are going to use a built-in CSV module.

Most of the time you need to read or write CSV files using Python, so you can use the Python built-in CSV module that provides approx all the functionality to read or write CSV files with the help of the Python programming language.

In this guide, we will only focus on reading CSV files in different formats with the help of examples.

Python CSV Module

CSV module in Python is a built-in module that means you don’t need to install it by using pip. It comes with Python by default. Python CSV module provides all the methods and properties to work with CSV files.

To use the Python CSV module in your code, you have to import it by using the import keyword.

import CSV

Use of Python CSV reader() function.

csv.reader(csvfile, dialect=’excel’, **fmtparams)

csv.reader() function is used to return a reader object that are capable of iterate and return one line of given CSV file.

Suppose, we have a CSV file named students.csv that contains the following contents.

id,first_name,last_name,roll_no,course
1,Vishvajit,Rao,12|BCA
2,Shivam,Kumar,13,BTech
3,Shreya,Singh,14,MCA
4,Shivam,,Rao,40,PHD

Example: Read CSV file using csv.reader()

import csv
with open('students.csv', 'r') as file:
    csvreader = csv.reader(file)
    for row in csvreader:
        print(row)

As you can see in the above example, we have used the Python open() built-in function to open the students.csv file.

CSV Files with custom delimeter

By default, a comma is used as a delimiter in a CSV file. However, some CSV files can use delimiters other than a comma.

Suppose the students.csv file use pipe ( | ) as a delimiter like below. Now, to read the students.csv file, we have to pass an additional parameter delimiter.

students.csv

id|first_name|last_name|roll_no|course
1|Vishvajit|Rao|12|BCA
2|Shivam|Kumar|13|BTech
3|Shreya|Singh|14|MCA
4|Shivam|Rao|4|PHD

Example: Reading CSV file with custom delimiter

import CSV
with open('students.CSV', 'r') as file:
    csvreader = CSV.reader(file, delimter='|')
    for row in CSVreader:
        print(row)

As you can see in the above example, we have passed an additional parameter delimiter = ‘|’ that helps to specify the csvreader object that the CSV file we are reading from, has pipe as a delimiter.

Read CSV files with initial space

Most of the CSV files contain space after a delimiter. When we use the default CSV.reader() function to read the CSV files, we will get the space along with output.

To avoid the initial space in the result, we have to pass skipinitialspace = True to the csv.reader() function.

students.csv

id| first_name| last_name| roll_no| course
1| Vishvajit| Rao| 12| BCA
2| Shivam| Kumar| 13| BTech
3| Shreya| Singh| 14| MCA
4| Shivam| Rao| 4| PHD

Example: Read CSV files with initial space

import csv
with open('students.csv', 'r') as file:
    csvreader = csv.reader(file, delimter='|', skipinitialspace = True)
    for row in csvreader:
        print(row)

Read CSV files with quotes

Some CSV files have quotes around each of the items. To read that type of CSV file we have to use quoting parameter in csv.reader() function.

students.csv

id| first_name| last_name| roll_no| course
1| Vishvajit| Rao| 12| BCA
2| Shivam| Kumar| 13| BTech
3| Shreya| Singh| 14| MCA
4| Shivam| Rao| 4| PHD

Example: Read CSV files with quotes

import csv 
with open('students.csv', 'r') as file:
    csvreader = csv.reader(file, quoting=csv.QUOTE_ALL, skipinitialspace=True)
    for row in csvreader:
        print(row)

As you can see in the above example, we have passed the csv.QUOTE_ALL to the quoting parameter.
csv.QUOTE_ALL tells the reader object that all the items are present inside double-quotes.

There are three constants are available to pass the quoting parameter.

  • csv.QUOTE_MINIMUML:- This contant specify reader object CSV files has quotes arround those item that contain special characters such as delimiter, quotechar and lineterminator.
  • csv.QUOTE_NONNUMERIC:- This constant specify reader object that CSV file contain quotes arround only the non-numeric characters.
  • csv.QUOTE_NONE:- Specifies the reader object that none of the entries have quotes around them.

Dialect in CSV Module

As you can see in the above Python code, we have pass multiple parameters inside csv.reader() function. This practice is good when you working with one or two files but sometimes we have multiple CSV files, then this practice looking weird.

To solve this problem, Python CSV Module provide a csv.register_dialect() function which support multiple parameters, that pass into csv.reader() function.

Example

import csv
csv.register_dialect('MyDialect', quoting=csv.QUOTE_ALL, skipinitialspace=True, delimiter='|')
with open('students.csv', 'r') as file:

    # Delete dialect
    csv.unregister_dialect('MyDialect') 

    # Access all the delimiter
    dial = csv.get_dialect('MyDialect')
    print(dial.delimiter)

    # List all the dialect
    print(csv.list_dialects())

    csv.field_size_limit(5)
    csvreader = csv.reader(file, dialect='MyDialect')
    for row in csvreader:
        print(row)

Read CSV files using csv.DictReader()

The object of csv.DictReader() class is used to read CSV file as a dictionary.

students.csv

id|first_name|last_name|roll_no|course
1|Vishvajit|Rao|12|BCA
2|Shivam|Kumar|13|BTech
3|Shreya|Singh|14|MCA
4|Shivay|Rao|40|PHD

Example: Read CSV file using csv.DictReader()

import csv
with open('students.csv', 'r') as file:
    csvreader = csv.DictReader(file, delimiter='|', fieldnames=None)
    for row in csvreader:
        print(dict(row))

Output

{'id': '1', 'first_name': 'Vishvajit', 'last_name': 'Rao', 'roll_no': '12', 'course': 'BCA'}
{'id': '2', 'first_name': 'Shivam', 'last_name': 'Kumar', 'roll_no': '13', 'course': 'BTech'}
{'id': '3', 'first_name': 'Shreya', 'last_name': 'Singh', 'roll_no': '14', 'course': 'MCA'}
{'id': '4', 'first_name': 'Shivam', 'last_name': 'Rao', 'roll_no': '4', 'course': 'PHD'}

The csv.DictReader() returned an OrderedDict type for each row. That’s why we used the Python dict() function to convert each row to a dictionary.

Example

import csv
with open('students.csv', 'r') as file:
    csvreader = csv.DictReader(file, delimiter='|', fieldnames=None)

    # csvreader.__next__ Return the next row of the reader’s iterable object
    print(dict(csvreader.__next__()))
    

    for row in csvreader:
        print(dict(row))

    # csvreader.fieldnames return the fieldnames as list
    print(csvreader.fieldnames)

    # csvreader.line_num return the number of line that inlcude in csv file.
    print(csvreader.line_num)

Output

{'id': '1', 'first_name': 'Vishvajit', 'last_name': 'Rao', 'roll_no': '12', 'course': 'BCA'}
{'id': '2', 'first_name': 'Shivam', 'last_name': 'Kumar', 'roll_no': '13', 'course': 'BTech'}
{'id': '3', 'first_name': 'Shreya', 'last_name': 'Singh', 'roll_no': '14', 'course': 'MCA'}
{'id': '4', 'first_name': 'Shivam', 'last_name': 'Rao', 'roll_no': '4', 'course': 'PHD'}
['id', 'first_name', 'last_name', 'roll_no', 'course']
5

Conclusion

So, In this article, we have seen all about the Python CSV Module to read CSV files in Python. CSV module in Python is one of the most popular modules to work with CSV file data.

In the next tutorial, we will explain all the possible ways to write CSV files using Python CSV built-in module. If you like this article, please share and keep visiting for further Python tutorials.

Reference:- Click Here

Thanks for reading

Previous Article
Next Article

Related Posts