Python CSV

Table of Contents

Introduction

This post will help you configure your environment to write a Python program whose purpose is to read or write a CSV file.

In your life as a developer, you will surely have to write or read a CSV file.
A comma-separated values (CSV) file is a simple text file that uses commas to separate values.
In reality, the values can also be separated with other characters such as the semicolon (;) or the tab (\t).

Python CSV module

Python already includes a module for reading and writing CSV files which is called just csv.
So if you are using Python version 3.x you will not need to install any additional packages.

However, if the project is more complex than simply reading or writing CSV files, my advice is to create a virtualenv.
If you don’t know how, this post will help you.

To start using the Python csv module just import it like this:

import csv

Python CSV – Reading CSV file

This section will show you how to create a Python script to read data from a CSV file.
First of all, let’s create a CSV file to use as example and then let’s take care of the code.

Prepare data

As mentioned earlier, a CSV file is a simple text file whose values are separated by commas.
Before we see the Python code that reads data from a csv file, let’s prepare one that you can use as an example.

To do this, simply open an editor and copy and paste this content in a file called laptop.csv:

Id,Brand,RAM (GB),Number of cores
1,Asus,8,8
2,HP,16,8
3,Dell,16,16

Opening this file with LibreOffice Calc or Excel, the output looks like this:

laptop.csv file used in python script
laptop.csv file

Read data

To read the CSV file created in the previous section, we will use the Python csv module.

First of all, to read a CSV file in Python we must first open the file using the with statement, as with any other type of file.

with open("<filepath>", "<opening_mode>") as csv_file:
    # here the file is open
    ....
# here the file is closed

After that, we use the Python csv module reader to read each line of the file.

csv_reader = csv.reader(csv_file)

Now we can iterate the result of csv.reader to read each line of the CSV file.

for row in csv_reader:
        print(row)

Putting it all together, this is what we get:

import csv
with open("laptop.csv", "r") as csv_file:
    csv_reader = csv.reader(csv_file)
    for row in csv_reader:
        print(row)

Output

Python script output to read a csv file
Python script output to read a csv file

Read data using custom delimiter

The previous example showed how to read a CSV file with the comma as a delimiter.
Some CSV files may not have a comma but a semicolon, tab or other symbol.
Now I will show you what needs to be changed in the above script in case there was a delimiter other than comma.

import csv
with open("laptop.csv", "r") as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=";")  # use "\t" for tabs
    for row in csv_reader:
        print(row)

As you can see from the code above, the csv.reader accepts a parameter called delimiter.
The default value is the comma (,) but you can specify another symbol to read the CSV file.

At this link you can find the Python documentation for csv.reader function.

DictReader class

As you have probably already noticed, csv.reader returns lines in the form of lists.
Depending on the project, it may be convenient to have lines as dictionaries and not as lists.
For this type of task we use the DictReader class.

Below, find the same code as before but using the DictReader class:

import csv
with open("laptop.csv", "r") as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader:
        print(row)

Remember that if you are using a version of Python lower than 3.8 you need to cast the line to dict, otherwise the type will be OrderedDict.

import csv
with open("laptop.csv", "r") as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader:
        print(dict(row))

Python CSV – Writing CSV file

To write a CSV file, we will use the Python csv module.

First of all, to write a CSV file in Python we must first open the file and, as for reading, we do this using the with statement.

with open("<filepath>", "<opening_mode>") as csv_file:
    # here the file is open
    ....
# here the file is closed

After that we use the Python csv module writer to write each line of the file.

csv_writer = csv.writer(csv_file)

Now, to add a new row in the CSV file it is possible to use the writerow method.

csv_writer.writerow(["1", "first", "example", "row"])
csv_writer.writerow(["2", "second", "example", "row"])
csv_writer.writerow(["3", "third", "example", "row"])

Putting it all together, this is what we get:

import csv
with open("example.csv", "w") as csv_file:
    csv_writer = csv.writer(csv_file)
    csv_writer.writerow(["1", "first", "example", "row"])
    csv_writer.writerow(["2", "second", "example", "row"])
    csv_writer.writerow(["3", "third", "example", "row"])

Output

Python script output to write a csv file
Python script output to write a csv file

The writerow method can be cumbersome to use if there are many lines to insert.
In this case I suggest you to use the writerows method, which accepts as a parameter a list of lists that correspond to the single lines.
Now let’s see how to create the same file but using the writerows method:

import csv
rows_to_insert = [["1", "first", "example", "row"], ["2", "second", "example", "row"], ["3", "third", "example", "row"]]
with open("example.csv", "w") as csv_file:
    csv_writer = csv.writer(csv_file)
    csv_writer.writerows(rows_to_insert)

Write data using custom delimiter

The writer creates comma delimited CSV files by default but of course you can customize this setting by using the delimiter parameter as shown in the example below.

import csv
rows_to_insert = [["1", "first", "example", "row"], ["2", "second", "example", "row"], ["3", "third", "example", "row"]]
with open("example.csv", "w") as csv_file:
    csv_writer = csv.writer(csv_file, delimiter=";")
    csv_writer.writerows(rows_to_insert)

Like the csv.reader, also the csv.writer accepts a parameter called delimiter which default value is the comma (,) but you can specify another symbol to write the CSV file.

At this link you can find the Python documentation for csv.writer function.

DictWriter class

It may happen that it is more convenient for you to write a CSV file by schematizing the rows as dictionaries and not as lists.
For this kind of tasks I recommend you to use the DictWriter class.
In the following code you will find the same example as in the previous sections but with the DictWriter class.

import csv
with open("example.csv", "w") as csv_file:
    fieldnames = ["id", "first_column", "second_column", "third_column"]
    csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    csv_writer.writerow({"id": "1", "first_column": "first", "second_column": "example", "third_column": "row"})
    csv_writer.writerow({"id": "2", "first_column": "second", "second_column": "example", "third_column": "row"})
    csv_writer.writerow({"id": "3", "first_column": "third", "second_column": "example", "third_column": "row"})

There are two things to note.
First of all, the DictWriter class needs a mandatory parameter called fieldnames which represents the header of the CSV file and secondly, that the writerow method accepts a dictionary and no longer a list.

Conclusion

You have now learned how to read and write a CSV file using Python and without installing additional packages!
In this GitHub repository you can find the code that has been written in this post; I invite you to clone it and play with it a bit.
If you have any errors do not hesitate to leave me a comment below, I will be happy to help you.

Leave a Comment

Your email address will not be published. Required fields are marked *