Read and write CSV file using python

Satya Python solution ,

Read and write CSV file using python : CSV file, which is comma separated file, is the most popular form of data storage spreadsheet. In this post, I will explain you how we can read and write CSV file using python module.

Read and write CSV file using python module.

CSV module has different classes to read csv file and also has function to write it.

Reading csv file using csv module.

Below example demonstrate reading csv file using csv.reader function. Here, it opens csv file in read mode(specified ‘r’ flag). This is open as csvfile0 file object and has object support iterator, which iterate through each line of the file every time next methods get called.

If it is file object then use ‘b’ flag. This reader function accept three argument. First is csv file object, second is delimiter, and third is quotechar. Specify quotechar if your data contain comma(should not be delimiter) and it is inside quote.

csv.reader read csv file object and return list containing strings.

import csv

with open("../csv_text.txt", 'rb') as csvfileO:
csvReadObj=csv.reader(csvfileO, delimiter=',',quotechar='|')

for row in csvReadObj:
print (row)


I have used below csv file to demonstrate reading of data by preceding code. Run this script on UNIX shell or on window platform. As a result, it will print each row in list form.

>cat ../csv_text.txt 
|ID no|,Name,salary
2314, John,|70,000|
2315, harry,|79,000|
2134, Marry,|87,000|
>python csv_reader.py 
['ID no', 'Name', 'salary']
['2314', ' John', '70,000']
['2315', ' harry', '79,000']
['2134', ' Marry', '87,000']

Writing csv file using csv.writer function.

Use open function to open csv file as file object in write mode. Open function accept two argument. First is csv file, and the second is ‘wb’. ‘w’ specfiy write mode while ‘b’ specify file object. csv.writer file uses file object, which has writer method,  This write text data in csv format. Also, csv.writer accept two more argument. First is delimiter and the second is to define quotechar. csv.writer return object reference. Use this to call writerow method. You can specify data as argument to this method. This will write the data into the file.

import csv

with open('data.csv', 'wb') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
csvwriter.writerow(['something'] * 5 + ['Baked,Beans']) 
csvwriter.writerow(['fantastic', 'Lovely,Spam', '50,0000'])

Run preceding code and it will generate csv file. You can see the output, which contains comma in between. This is surrounded by quote (pipe).

>python csv.writer.py
>cat data.csv
something,something,something,something,something,|Baked,Beans|
fantastic,|Lovely,Spam|,|50,0000|

You May Also Like..

How to concatenate string in Python

How to concatenate string in Python

How to concatenate string in Python: Concatenating a string is very important feature in any computer programming language. Python provide […]

Multi threading program in python

Multi threading program in python

Multi threading program in python: Multi-Thread is a multiple processes running concurrently in one CPU core processor. It is different […]

How to create socket in Python programming

How to create socket in Python programming

How to create socket in Python programming: Socket is network interface, where two nodes in a network connect and communicate. […]

Leave a Reply

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