CSV Files in Python

What is a CSV File?

CSV stands for Comma Separated Values, is one of the popular ways of storing tabular data in the file. Python provides an easy way to work with CSV file. It has a csv module to read and write data in the CSV file.

Let us consider an example of a CSV file with three attributes, SN, Name, and City. We can visualize the data in table format as below:_

SNNameCity
1John DoeNew York
2Freddie MooreTexas
3Troy JacksonCalifornia

So, to store this data, we make a csv file named people.csv and store the content as comma separated.

SN, Name, City
1, John Doe, New York
2, Freddie Moore, Texas
3, Troy Jackson, California

What are CSV methods provided by Python?

Python provides a set of methods to work on with CSV files. It provides a csv module that can be imported into your application and use the following methods to start working with CSV files.

MethodDescription
csv.readerreturns a reader object which iterates over lines of a CSV file
csv.writerreturns a writer object which writes data into CSV file
csv.register_dialectregisters a CSV dialect
csv.unregister_dialectunregisters a CSV dialect
csv.get_dialectreturns a dialect with the given name
csv.list_dialectsreturns all registered dialects
csv.field_size_limitreturns the current maximum field size allowed by the parser

How to read a CSV File in Python?

To read a csv file in Python, we use csv.reader() module. Let us use the people.csv created as above containing three names and address of people.

We get the following output:-

[‘SN’, ‘ Name’, ‘ City’]
[‘1’, ‘ John Doe’, ‘ New York’]
[‘2’, ‘ Freddie Moore’, ‘ Texas’]
[‘3’, ‘ Troy Jackson’, ‘ California’]
To use a custom delimiter, in the above case we have used “,” as a delimiter, but there may be a situation where other delimiters might be used. In that case, we can use the following syntax:-
csv.reader(csvFile, delimiter=”;”)

How to write in a CSV File in Python?

In python, we use csv.writer module to write in a csv file.

The resulting output is the file named person.csv with the following content

Name of Person,Age
Peter,22
Jasmine,21
Sam,24

How to modify existing rows in a CSV file using Python?

To update any row, we will need to update the whole CSV file. The use of the writerows() method for csv.writer() provides updating of the list with ease. Remember, the indexing of the list starts with 0. In the following example, we are going to update the 2nd-row person name from Freddie Moore to Marie keeping the address same.

This outputs the modified file people.csv with the following content:-

SN, Name, City
1, John Doe, New York
2, Marie, Texas
3, Troy Jackson, California

How to append new rows to CSV file in Python?

We can open a file in append mode and write csv row to it. Let’s append a new user “Danny” with address “New York” with the help of following code:-

This results in an updated version of the people.csv file:-

SN, Name, City
1, John Doe, New York
2, Marie, Texas
3, Troy Jackson, California
4, Danny, New York

How to write CSV file into a Dictionary in Python?

Using DictWriter() class of CSV module, we can write a CSV file into a dictionary. It works similar to the writer() function but creates an object which maps data into a dictionary. The keys are given by the field names parameter.

The output results in peak.csv file with the following content:-

mountain,height
Everest,8848
K2, 8611
Kanchenjunga,8586

How to use Custom dialect in Python?

A dialect is a class of CSV module which helps to define parameters for reading and writing CSV. It allows you to create, store, and re-use various formatting parameters for your data.

SHARE CSV Files in Python

You may also like...

Leave a Reply

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

Share