Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Python CSV Module – Read and Write to CSV Files

Python Csv Module

In this tutorial, we’ll look at the Python CSV Module, which is very useful for csv file processing.

Using this module, which comes bundled with Python, we can easily read and write to CSV files.

Let’s get started!


Using the Python csv module

We must import the csv module to use relevant methods.

import csv

Now, depending on what you want to do, we can read or write to csv files using appropriate objects.

Let’s look at reading csv files first.

Reading from csv files using csv.reader()

To read from a csv file, we must construct a reader object, which will then parse the file and populate our Python object.

Python’s csv module has a method called csv.reader() which will automatically construct the csv reader object!

We must call the csv.reader() method on an already opened file object, using open().

import csv
reader = csv.reader(file_object)

Normally, the recommended approach is to enclose everything using a with context manager.

You can do something similar to this:

import csv

# Open the csv file object
with open('sample.csv', 'r') as f:
    # Construct the csv reader object from the file object
    reader = csv.reader(f)

The reader object will be an iterable consisting of all the rows in the csv file. By default, each row will be a Python List, so it will be very convenient for us!

So you can directly print the rows using the for loop as shown below:

for row in reader:
    print(row)

Alright. Now that we have a basic template code, let’s print the contents of the below file using csv.reader().

Let’s consider sample.csv to have the below content.

Club,Country,Rating
Man Utd,England,7.05
Man City,England,8.75
Barcelona,Spain,8.72
Bayern Munich,Germany,8.75
Liverpool,England,8.81

Now, let’s run code:

import csv
with open('sample.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)

Output

['Club', 'Country', 'Rating']
['Man Utd', 'England', '7.05']
['Man City', 'England', '8.75']
['Barcelona', 'Spain', '8.72']
['Bayern Munich', 'Germany', '8.75']
['Liverpool', 'England', '8.81']

Okay, so we do get all the rows. Here, as you can see, csv has given us the space after the comma.

If you want to parse individual words, by separating using the whitespace character, you can simply pass it to csv.reader(delimiter=' ') as a delimiter character.

Let’s try out the modified code now:

import csv

with open('sample.csv', 'r') as f:
    reader = csv.reader(f, delimiter=' ')
    for row in reader:
        print(row)

Output

['Club,', 'Country,', 'Rating']
['Man', 'Utd,', 'England,', '7.05']
['Man', 'City,', 'England,', '8.75']
['Barcelona,', 'Spain,', '8.72']
['Bayern', 'Munich,', 'Germany,', '8.75']
['Liverpool,', 'England,', '8.81']

Indeed, we’ve now split the words, so Man Utd becomes Man and Utd.

Similarly, if you want to parse delimited content, simply pass that character as a delimiter to csv.reader().

Let’s now look at writing to a csv file.


Writing to csv files using csv.writer()

Analogous to the csv.reader() method for reading, we have the csv.writer() method for writing to files.

This will return a writer object which we can use to write rows to our destination file.

Let’s look at how we can use this. First, create the writer object:

import csv

with open('output.csv', 'w') as f:
    writer = csv.writer(f)

We can now use the writer.writerow(row) method to write a row. Here, similar to the reader object, row is a list.

So, we can invoke it like this:

writer.writerow(['Club', 'Country', 'Rating'])

Let’s look run the whole program now:

import csv

with open('output.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerow(['Club', 'Country', 'Rating'])
    clubs = [['Real Madrid', 'Spain', 9.1], ['Napoli', 'Italy', 7.5]]
    for club in clubs:
        writer.writerow(club)

Let’s now look at output.csv.

Club,Country,Rating
Real Madrid,Spain,9.1
Napoli,Italy,7.5

Indeed, we have our rows on the output file!

NOTE: Similar to csv.reader(delimiter), we can also pass a delimiter character to write using csv.writer(delimiter)

If you observed closely, we have manually iterated through our list of rows (list of lists) and written each row one by one.

Turns out there is another method called writer.writerows(rows) which can directly write all our rows!

Let’s test it out. Delete output.csv and run the below code.

import csv

with open('output.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerow(['Club', 'Country', 'Rating'])
    clubs = [['Real Madrid', 'Spain', 9.1], ['Napoli', 'Italy', 7.5]]
    writer.writerows(clubs)

Output

Club,Country,Rating
Real Madrid,Spain,9.1
Napoli,Italy,7.5

We indeed get the same output as before!


Using csv.DictReader() and csv.DictWriter() to read and write to a csv as a Dictionary

Remember that when reading using the reader object, we got the objects row-wise, as a list?

If you want the exact column_name: row_name mapping, we can use the csv.DictReader class and get a Dictionary instead!

Let’s look at how we can read from a csv file into a dictionary.

import csv

with open("sample.csv", 'r') as file:
    csv_file = csv.DictReader(file)

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

Here, csv.DictReader() returns an iterable of OrderedDict() objects. We need to convert each OrderedDict row to a dict, using dict(row).

Let’s look at the output:

{'Club': 'Man Utd', ' Country': ' England', ' Rating': ' 7.05'}
{'Club': 'Man City', ' Country': ' England', ' Rating': ' 8.75'}
{'Club': 'Barcelona', ' Country': ' Spain', ' Rating': ' 8.72'}
{'Club': 'Bayern Munich', ' Country': ' Germany', ' Rating': ' 8.75'}
{'Club': 'Liverpool', ' Country': ' England', ' Rating': ' 8.81'}

Indeed, we have the column name as well as the row value!

Now, for writing to a csv file from a Dictionary, you have the csv.DictWriter() class.

This is almost the same as csv.write(), except that you’re writing from a dictionary instead of a list.

The syntax is a bit different though. We must specify the column names in advance, as part of our fieldnames.

We then need to write the first row (header) using writer.writeheader().

    fieldnames = ['Club', 'Country', 'Rating']
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writeheader()

Now, we can iterate through our list of dicts, which has the relevant information.

Let’s re-write our old writer example using csv.DictWriter().

import csv

with open('output.csv', 'w') as f:
    fieldnames = ['Club', 'Country', 'Rating']
    # Set the fieldnames
    writer = csv.DictWriter(f, fieldnames=fieldnames)

    # Write the header
    writer.writeheader()

    clubs = [{'Club': 'Real Madrid', 'Country': 'Spain', 'Rating': 9.1}, {'Club': 'Napoli', 'Country': 'Italy', 'Rating': 7.5}]

    for club in clubs:
        writer.writerow(club)

We’ll now get the same output as before, indicating that we’ve successfully written to the csv file using our csv.DictWriter() object!


Conclusion

Hopefully, you’ve understood how you can use the csv module to process csv files easily. We made it easy to read and write to/from csv files, using suitable objects.

References