Methods and Functions

Python CSV: Read and Write CSV files

CSV is an acronym used for Comma Separated Values. It is the most common import and export format for spreadsheets and databases and is supported by a wide range of applications. A CSV file stores tabular data in which each data field is separated by a delimiter which is comma in most cases, as the name suggests. A CSV file must be saved with the .csv file extension. This article will explain in detail how to read and write into csv files in python with the help of examples for each.

Working with CSV Files in Python

Python supports a module called csv which makes working with csv files a lot easier. There are certain methods supported by the csv module to provide read, write and many other abilities to users. To use these csv methods, we need to import the csv module in our python file using this syntax:

Import csv                                                                                                             .

Reading CSV files Using csv.reader()

The reader class from the csv module is used for reading data from a CSV file, we can use the csv.reader() function. At first, the inbuilt open() method in ‘r’ mode(specifies read mode while opening a file) is used to open the csv files which returns the file object then it is read by using the reader() method of the CSV module. The reader() function returns the reader object that iterates through all the lines in the given CSV file.

Syntax: csv.reader( csvFile, dialect=’excel’, **optional_params )

 Example: Consider a csv file named students_data.csv.

Name Age Grade
Apoyo 21 A
Animar 20 D
Bob 20 B

Here is the sample code to show the working of the reader() method. We will be reading the students_data.csv file above.

Code

import csv

                    

# opening the csv file

with open ('students_data.csv', mode ='r' ) as file:

# reading the csv file

csvFile = csv.reader(file)

for lines in csvFile:

print(lines)

Output

[[‘Name’, ‘Age’, ‘Grade’],

[‘Apoyo’, 21, ‘A’ ],

[‘Animar’, 20, ‘D’],

[‘Bob’, 20, ‘B’]]

Explanation:

In the above sample, we opened the students_data.csv file in reading mode using open() function. Then, we read the file using csv.reader() method which returns an iterable reader object named csvFile which is then iterated using a for loop to print the contents of each row. In the above example, we have used the csv.reader() function in default mode. We can pass optional parameters to the csv.reader() function, for example:

csvFile = csv.reader(file, delimiter = ‘\t’)

Writing CSV files Using csv.writer()

The csv.writer class from the csv module is used to insert data to a CSV file. User’s data is converted into a delimited string by the writer object returned by csv.writer(). The csv.writer class provides two methods for writing to CSV, namely, writerow() and writerows().

Syntax: csv.writer(csv_file, dialect=’excel’, **optional_params)

  • The writerow() method is used to write a single row at a time into a CSV file. We can write a field row using this method.

Syntax: writerow(fields)

  • The writerows() method is used to write multiple rows at a time i.e., it can be used to write the contents of a 2-dimensional list into a csv file. Row lists can be written using this method.

Syntax: writerows(rows)

EXAMPLE-1: Let’s take an example where we write data to a csv file named student_records.csv.

Code

                    

Import csv

field_entries = [‘Name’ , ‘Branch’ , ‘CGPA’ ]

rows_entries = [ [‘Vibhor Goyal’ , ‘Electrical’ , ‘9.3’],

[‘Tarun Trivedi’ , ‘Civil’ , ‘8.5’],

[‘Shreya Sharma’ , ‘IT’ , ‘9.8’ ] ]

file_name = “student_records.csv”

with open (file_name, ‘w’) as csv_file:

csv_writer = csv.writer(csv_file)

csvwriter.writerow(field_entries)

csvwriter.writerows(rows_entries)

 

Output

Name Branch CGPA
Vibhor Goyal Electrical 9.3
Tarun Trivedi Civil 8.5
Shreya Sharma IT 9.8

 EXAMPLE-2: Let’s take an example where we add an optional parameter delimiter = ‘\t’ in the csv.writer() method. In this case the separator will be tab space instead of comma.

Code

                    

import csv

with open(‘protagonist_roles.csv’, ‘w’) as file:

writer_object = csv.writer(file, delimiter = ‘\t’)

writer.writerow([“SNo.”, “Movie_Name”, “Protagonist”])

writer.writerow([1, “Mission impossible”, “Ethan Hunt”])

writer.writerow([2, “Star wars”, “Luke Skywalker”])

Python csv.DictReader() Class

The csv.DictReader() class functions like a normal reader but maps the read information into a dictionary. The first row of the csv file infer the keys for the dictionary that can be passed in with the fieldname parameters.

Syntax: csv.DictReader(file, fieldnames=None, restkey=None, restval=None, dialect=’excel’, *arguments, **keywords)

Example: Let’s take a sample code to see the working of csv.DictReader() function. Consider a csv file named students_data.csv.

Name Age Grade
Apoyo 21 A
Animar 20 D
Bobby 20 B

Code

 

                    

import csv

with open(“students_data.csv”, ‘r’) as csv_file:

csv_DictReader_object = csv.DictReader(csv_file)

for row in csv_DictReader_object:

print(dict(row))

Output

{‘Name’: ‘Apoyo’, ‘ Age’: ‘ 21’, ‘ Grade’: ‘ A’}

{‘Name’: ‘Animar’, ‘ Age’: ‘ 20’, ‘ Grade’: ‘ D’}

{‘Name’: ‘Bobby’, ‘ Age’: ‘ 20’, ‘ Grade’: ‘ B’}

 

Explanation

As we can see in the above example, the entries of the first row are the dictionary keys and the entries in other rows are the dictionary values. We opened the file students_data using the open() method. Then we used the csv.DictReader() method to read the csv file which returns the csv DictReader object named csv_DictReader_object. We then use the dict() method to create dictionaries inside the for loop and print them.

Python csv.DictWriter() Class

We can also write dictionaries to csv files by using the csv.DictWriter class provided by the CSV module. The csv.DictWriter() class functions like a normal writer, it returns a writer object which maps dictionaries into output rows.

The syntax for using csv.DictWriter() class is:

csv.DictWriter(csv_file, field_names, restval=”, extrasaction=’raise’, dialect=’excel’, *args, **kwds)

The csv.DictWriter provides two methods for writing to CSV, namely writeheader() and writerows().

  • writeheader() method adds the first row of csv file inferring from pre-specified fieldnames i.e., keys of the dictionary.

Syntax: writeheader()

  • writerows() writes all the rows of the csv file adding all the values and skipping the keys

Syntax: writerows(my_dictionary)

 Example: Let’s take a sample example to understand the implementation of csv.DictWriter() class and writeheader() and writerows() method.

Code

                    

import csv

my_dict = [ {‘branch’: ‘Civil Engineering’, ‘cgpa’: ‘9.2’, ‘name’: ‘Apoyo’, ‘year’: ‘4’},

{‘branch’: ‘Electrical Engineering’, ‘cgpa’: ‘8.6’, ‘name’: ‘Animar’, ‘year’: ‘3’},

{‘branch’: ‘Information Technology’, ‘cgpa’: ‘8.9’, ‘name’: ‘Bob’, ‘year’: ‘1’},

{‘branch’: ‘Mechanical Engineering’, ‘cgpa’: ‘7.2’, ‘name’: ‘Steve’, ‘year’: ‘3’}]

 

field_names = [‘name’, ‘branch’, ‘year’, ‘cgpa’]

 

file_name = student_records.csv

 

with open(file_name, ‘w’) as csv_file:

writer_obj = csv.DictWriter(csv_file, fieldnames = field_names)

writer.writeheader()

writer.writerows(my_dict)

Output

name branch year cgpa
Apoyo Civil Engineering 4 9.2
Animar Electrical Engineering 3 8.6
Bob Information Technology 1 8.9
Steve Mechanical Engineering 3 7.2

 Using the Pandas library to Handle CSV files

Pandas library in python can be used for efficiently dealing with CSV files if there is a huge set of data. For using the pandas library, we first have to import it and that can be done using the syntax:

import pandas as pd

Reading the csv file using Pandas library

To read the file using Pandas library we can use read_csv() method as shown in the sample code below, it reads people_records.csv from the current directory.

import pandas as pd

pd.read_csv(“people_records.csv”)

Writing the csv file using Pandas library

The DataFrame supports a to_csv() method which can be called to write a csv file. So to write a file using the pandas library we first have to create a DataFrame using the pd.DataFrame() method. Then this DataFrame will call the to_csv() function to write in people_records.csv file.

Example:

import pandas as pd

df = pd.DataFrame([[‘Apoyo’, 21], [‘Animar’, 20]], columns = [‘Name’, ‘Age’])

df.to_csv(‘people_records.csv’)

Frequently Asked Questions

Q.1. How do I create a CSV file in Python?

Answer: To create a CSV file in python, we use a comma character as separator. Comma is the default delimiter of CSV files.

Here is an example of creating a CSV file in Python:

                    

import csv

with open(‘persons_file.csv’, ‘wb’) as csv_file:

filewriter = csv.writer(csv_file, delimiter=’,’,

quotechar=’|’, quoting=csv.QUOTE_MINIMAL)

filewriter.writerow([‘Name’, ‘Hobby’])

filewriter.writerow([‘Bob’, ‘Painting’])

filewriter.writerow([‘Apoyo’, ‘Singing’])

filewriter.writerow([‘Paul’, ‘Writing fictional stories’])

Now if you will run this code it will fill persons_file.csv with the following content:

Name, Hobby

Bob, Painting

Apoyo, Singing

Paul, Writing fictional stories

You can import this persons_file.csv file in any office program of your choice.

Name Hobby
Bob Painting
Apoyo Singing
Paul Writing fictional stories

Hence, a CSV file named persons_file.csv is created in python.

Q.2. How do I read a csv file in Python?

Answer: The reader class from the csv module is used for reading data from a CSV file, we can use the csv.reader() function. At first, the inbuilt open() method in ‘r’ mode(specifies read mode while opening a file) is used to open the csv files which returns the file object then it is read by using the reader() method of the CSV module. The reader() function returns the reader object that iterates through all the lines in the given CSV file.

Syntax: csv.reader( csvFile, dialect=’excel’, **optional_params )

Example: Consider a csv file named students_data.csv.

Name Age Grade
Apoyo 21 A
Animar 20 D
Bob 20 B

Here is the sample code to show the working of the reader() method.

Code

                    

 import csv

# opening the csv file

with open (‘students_data.csv’, mode =’r’ ) as file:

# reading the csv file

csvFile = csv.reader(file)

for lines in csvFile:

print(lines)

Output

[[‘Name’, ‘Age’, ‘Grade’],

[‘Apoyo’, 21, ‘A’ ],

[‘Animar’, 20, ‘D’],

[‘Bob’, 20, ‘B’]]

Explanation:

In the above sample, we opened the students_data.csv file in reading mode using open() function. Then, we read the file using csv.reader() method which returns an iterable reader object named csvFile which is then iterated using a for loop to print the contents of each row. In the above example, we have used the csv.reader() function in default mode. We can pass optional parameters to the csv.reader() function, for example:

csvFile = csv.reader(file, delimiter = ‘\t’)

We can also write dictionaries to csv files by using the csv.DictWriter class provided by the CSV module. The csv.DictWriter() class functions like a normal writer, it returns a writer object which maps dictionaries into output rows.

The syntax for using csv.DictWriter() class is:

csv.DictWriter(csv_file, field_names, restval=”, extrasaction=’raise’, dialect=’excel’, *args, **kwds)

The csv.DictWriter provides two methods for writing to CSV, namely writeheader() and writerows().

  • writeheader() method adds the first row of csv file inferring from pre-specified fieldnames i.e., keys of the dictionary.

Syntax: writeheader()

  • writerows() writes all the rows of the csv file adding all the values and skipping the keys

Syntax: writerows(my_dictionary)

Q.3. What is CSV in Python?

Answer: CSV is an acronym used for Comma Separated Values. It is the most common import and export format for spreadsheets and databases and is supported by a wide range of applications. A CSV file stores tabular data in which each data field is separated by a delimiter which is comma in most cases, as the name suggests. A CSV file must be saved with the .csv file extension.

Q.4. How do I use CSV data in Python?

Answer: To use CSV data in python, we have to first import the csv module using import csv.  Consider a csv file named students_data.csv and let us see an example of how we can use it’s data in Python.

Name Age Grade
Apoyo 21 A
Animar 20 D
Bob 20 B

Code

                    

import csv

# opening the csv file

with open (‘students_data.csv’, mode =’r’ ) as file:

# reading the csv file

csvFile = csv.reader(file)

for lines in csvFile:

print(lines)

Output

[[‘Name’, ‘Age’, ‘Grade’],

[‘Apoyo’, 21, ‘A’ ],

[‘Animar’, 20, ‘D’],

[‘Bob’, 20, ‘B’]]

Hence now this output data from the students_data.csv file can be used.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.