Python CSV Read Write

Filed Under: Python

In this tutorial we will learn about the python csv module that already exist with the python. In our previous tutorial, we have seen python mysql example.

Python CSV

CSV stands for comma separated values. In this tutorial we will see how to read and write CSV files in python. Python provides a module named csv, using this we can do several operations on the csv files.

Python CSV file

As I said before CSV is a file format. Python CSV module will help to read and write in the csv file.

The following is an example of csv file. The file name is employeeInfo.csv that is taken from an excel sheet containing the information of employee name, department and email address.

employeeInfo.csv


Employee Name,Department,Email Address
Rizvi,MEC,rizvy@yahoo.com
Mamun,EECE,mamun@hotmail.com
Shamsujjaman,CSC,shams@gmail.com
Anika,ECE,anika@gmail.com
Zinia,CSE,xinia@yahoo.com
Nazrul,AE,nazrul@hotmail.com

We have to keep this csv file in the same directory from where we want to access this file using python.

Python Read CSV

We can read the contents of csv file as followings with the help of csv.reader() method.


#importing csv
import csv

#openning the csv file which is in the same location of this python file
File = open('employeeInfo.csv')

#reading the File with the help of csv.reader()
Reader = csv.reader(File)

#storing the values contained in the Reader into Data
Data = list(Reader)

#printing the each line of the Data in the console
for data in Data:
   print(data)
File.close()

Below image shows the output produced by above python csv read example program.

python csv read

So we have read our csv file. What if we want to get the specific columns like only name and email address. Then we have to do as following:


#importing csv
import csv

#openning the csv file which is in the same location of this python file
File = open('employeeInfo.csv')

#reading the File with the help of csv.reader()
Reader = csv.reader(File)

#storing the values contained in the Reader into Data
Data = list(Reader)

#printing the 0th and 2nd column of each line of the Data in the console
for data in Data:
   print(data[0],' | ', data[2])
File.close()

It will output only the name and email address of the employee.

python read csv columns

Python CSV Writer

You can also write in csv file using python csv module. To write in the file you can open the file in write mode or you may open the file in append mode.

Then you have to use python csv.writer() to write in the csv file. The following is a example which writes in a csv file named output.csv.


#importing csv
import csv
# opening a file in write mode and newline = ''
# otherwise output.csv will contain two newline after writing each line.
File = open('output.csv', 'w', newline ='')

# setting the writer to the File
Writer = csv.writer(File)

# writing some values
Writer.writerow(['Yantai' , 'Resturant'])
Writer.writerow(['Convension Hall' , 'Community Center'])
Writer.writerow(['Lalbag Kella' , 'Historical Architecture'])

# closing the file
File.close()

Now you will see a file named output.csv in the same directory. Open that file, and you will find the values that you have wrote in it.

python csv writer

To know more about python csv I should recommend you to visit the official website.

Leave a Reply

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

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