Python CSV Files - How To Read and Write CSV Files in Python

Published December 10, 2021

When building python programs, you'll need more than just a keyboard and terminal to get information into and out of your python programs. It's normal practice to exchange data between programs by using text files. For sharing information, CSV is a common format. 

You don't have to write your own CSV parser in Python, which is a big plus. As an alternative, you may choose from a wide range of ideal libraries. In general, the CSV library is one of the most often used, whereas Pandas may be used when you need to analyze large datasets.

In this tutorial, we'll look at how to read and write CSV files in Python. This course will cover all of the important CSV libraries in Python. So, without further ado, let's get started.


What is a CSV file?

CSV is simply data in a table where the values are separated using separators. These separators are known as delimiters. Commas are one of the numerous delimiters, but they are not the only ones. The semicolon (;), the colon (:), and the tab (t) characters can all be used as well.


An example of the CSV file

Let’s take a look at the following CSV file example containing names:


First name, last name, Family name

David, Paul, Kingsley

Paul, Antony, Pius

Nancy, Mary, Pauline.


From the example above, every row represents a new line and every column is separated using a comma. 


Reading and Writing the CSV files

There are two ways in which you can read and write the CSV files:

  1. Using the CSV libraries

  2. Using  panda libraries


Reading  and writing CSV files  with CSV library

How  to read CSV files

Reading the CSV files is pretty much easier than it sounds to be.  Just check the illustrated steps below:

  • Step 1: First of all, you will need to generate the reader object from which you will read the CSV files. To do that, a reader function will be used.

Now, the reader function is going to take every row of the file and then generate the list of all the columns. Now using python, choose the column that you would like to have for the variable data. Take a look at the python code snippet below:


import CSV

with open('X:\student.csv','rt')as f:

data = csv.reader(f)

for row in data:



  • Step 2: we now have the code to read our CSV file. If you execute the code, all the data in the student.csv files will be displayed as the results.


How to read CSV files as a Dictionary

To access the CSV files as a dictionary is also an easy process. Using the DictReader, your CSV files will be interpreted as a dictionary where the header row will be treated as the key and other rows will be treated as values.  For instance, take a look at the following code snippet.


import CSV

reader = csv.DictReader(open("students.csv"))

for raw in reader:



The above code will simply read the values in students.csv files. That’s much pretty easy.  


How to write CSV files

Here is how you can use python to write a CSV file:

  • Step 1: First prepare the set of data values to write to CSV files.

  • Step 2: Now, you will use the write () function to store data into your CSV files. To write the data over rows, you will need to use the writerow()function. Take a look at the following python code snippet to write data into “students.csv”.


import csv

with open('X:\writeData.csv', mode='w') as file:

writer=csv.writer(file,delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

writer.writerow(['Student name', 'course', 'school', 'year of study'])

writer.writerow(['Margaret', 'Music', 'Music production', '1'])

writer.writerow(['James', 'Psychology', 'Psychology', '2'])

writer.writerow(['Cate', 'Literature', 'Linguistics', '3'])



Reading  and Writing CSV file with Pandas library

Panda is an open-source library that you can import into your python to manipulate data. Pandas library is pretty easy to use, regardless of the operating system that you have installed on your PC.

Before you make use of the panda’s library, you’ll need to first install the Pandas library on your PC using the command <code>pip install pandas</code>. In windows PC, you will run the command on the command prompt while in Linux PC; you run the command on the terminal.


How to read CSV files with Pandas

The following two steps illustrate how you can read the CSV files with Pandas library:

  • Step 1:  Create a file to read and name it with a “.csv” extension. In this post, we are going to create a file containing student's data and name it  students.csv


Name, course, lecturer, year of study, marks

Michael, medicine, Paul, 4, 567

John, Psychology, Elias, 3, 689

Philip, Music, Dan, 1, 234

Michelle, Business, Jackie, 3, 456

Esther, Hospitality, Naomi, 2, 389


  • Step 2:  Now, we have our CSV file named “students.csv”, to read it we are going to use the pandas.read_csv () as shown in the code snippet below.


Import pandas

df = panda.read_csv(‘students.csv’)

Print (df)


  • Step 3: that’s all; the above code will read and display students.csv files on your screen.


How to write CSV files with Pandas

Just like reading the CSV files, writing CSV files with pandas is as easy as illustrated in the two steps below:

  • Step 1: First of all, create a DataFrame to create the CSV file in your python code.


From pandas import DataFrame

c = {‘Name’: [‘Mercy’,’James’,’Cate’,’Rose’],

‘Course’: [‘Music’,’Medicine’, ‘Literarure’, ‘Hospitality’], 

‘Lecturer’: [‘Carol’,’Ralf’,’Messi’,’Paul’],

‘Year of study’: [‘1’,’2’,’3’,’4’],

’Marks’: [‘234’,’456’,’643’, 432’]

} Df = DataFrame (c, columns – [‘Name’, ‘course’, ‘lecturer’, ‘year of study’, ‘marks’])

export_csv = df.to_csv (r’x:\pandaresult.csv’, index = none, header=True)

Print (df)


Step 2: That’s all; run the code and the CSV file is successfully created.



Finally, you can use the CSV method to read and write data in the CSV format. The CSV format enables your python programs to easily read large sets of data and process them relatively fast.

We have learned various functions and classes that enable you to read and write CSV files easily and we can conclude that CSV format is the best way of saving, viewing, and sharing large sets of data

Article Contributed By :