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.
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.
Employee Name,Department,Email Address Rizvi,MEC,email@example.com Mamun,EECE,firstname.lastname@example.org Shamsujjaman,CSC,email@example.com Anika,ECE,firstname.lastname@example.org Zinia,CSE,email@example.com Nazrul,AE,firstname.lastname@example.org
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
#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.
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,' | ', data) File.close()
It will output only the name and email address of the employee.
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
#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.
To know more about python csv I should recommend you to visit the official website.