Reading and Writing CSV File using Python

PythonProgrammingServer Side Programming

CSV (stands for comma separated values) format is a commonly used data format used by spreadsheets. The csv module in Python’s standard library presents classes and methods to perform read/write operations on CSV files.

writer()

This function in csv module returns a writer object that converts data into a delimited string and stores in a file object. The function needs a file object with write permission as a parameter. Every row written in the file issues a newline character. To prevent additional space between lines, newline parameter is set to ‘’.

The writer class has following methods

writerow()

This function writes items in an iterable (list, tuple or string) ,separating them nby comma character.

writerows()

This function takes a list of iterables as parameter and writes each item as a comma separated line of items in the file.

Following example shows use of write() function. First a file is opened in ‘w’ mode. This file is used to obtain writer object. Each tuple in list of tuples is then written to file using writerow() method.

>>> import csv
>>> persons=[('Lata',22,45),('Anil',21,56),('John',20,60)]
>>> csvfile=open('persons.csv','w', newline='')
>>> obj=csv.writer(csvfile)
>>> for person in persons:
obj.writerow(person)
>>> csvfile.close()

This will create ‘persons.csv’ file in current directory. It will show following data.

Lata,22,45
Anil,21,56
John,20,60

Instad of iterating over the list to write each row individually, we can use writerows() method.

>>> csvfile = open('persons.csv','w', newline='')
>>> obj = csv.writer(csvfile)
>>> obj.writerows(persons)
>>> obj.close()

read()

this function returns a reader object which returns an iterator of lines in the csv file. Using the regular for loop, all lines in the file are displayed in following example.

>>> csvfile=open('persons.csv','r', newline='')
>>> obj=csv.reader(csvfile)
>>> for row in obj:
print (row)
['Lata', '22', '45']
['Anil', '21', '56']
['John', '20', '60']

Since reader object is an iterator, built-in next() function is also useful to display all lines in csv file.

>>> csvfile = open('persons.csv','r', newline='')
>>> obj = csv.reader(csvfile)
>>> while True:
try:
row=next(obj)
print (row)
except StopIteration:
break

The csv module also defines a dialect class. Dialect is set of standards used to implement CSV protocol. The list of dialects available can be obtained by list_dialects() function.

>>> csv.list_dialects()
['excel', 'excel-tab', 'unix']

DictWriter()

This function returns a DictWriter object. It is similar to writer object, but the rows are mapped to dictionary object. The function needs a file object with write permission and a list of keys used in dictionary as fieldnames parameter. This is used to write first line in the file as header.

writeheader()

This method writes list of keys in dictionary as a comma separated line as first line in the file.

In following example, a list of dictionary items is defined. Each item in the list is a dictionary. Using writrows() method, they are written to file in comma separated manner.

>>> persons=[{'name':'Lata', 'age':22, 'marks':45}, {'name':'Anil', 'age':21, 'marks':56}, {'name':'John', 'age':20, 'marks':60}]
>>> csvfile=open('persons.csv','w', newline='')
>>> fields=list(persons[0].keys())
>>> obj=csv.DictWriter(csvfile, fieldnames=fields)
>>> obj.writeheader()
>>> obj.writerows(persons)
>>> csvfile.close()

The file shows following contents.

name,age,marks
Lata,22,45
Anil,21,56
John,20,60

DictReader()

This function returns a DictReader object from the underlying CSV file. As in case of reader object, this one is also an iterator, using which contents of the file are retrieved.

>>> csvfile = open('persons.csv','r', newline='')
>>> obj = csv.DictReader(csvfile)

The class provides fieldnames attribute, returning the dictionary keys used as header of file.

>>> obj.fieldnames
['name', 'age', 'marks']

Use loop over the DictReader object to fetch individual dictionary objects.

>>> for row in obj:
print (row)

This results in following output.

OrderedDict([('name', 'Lata'), ('age', '22'), ('marks', '45')])
OrderedDict([('name', 'Anil'), ('age', '21'), ('marks', '56')])
OrderedDict([('name', 'John'), ('age', '20'), ('marks', '60')])

To convert OrderedDict object to normal dictionary, we have to first import OrderedDict from collections module.

>>> from collections import OrderedDict
>>> r=OrderedDict([('name', 'Lata'), ('age', '22'), ('marks', '45')])
>>> dict(r)
{'name': 'Lata', 'age': '22', 'marks': '45'}

In this article features of csv module have been explained.

raja
Published on 07-Dec-2018 06:58:08
Advertisements