Reading and Writing CSV Files
Reading and Writing CSV Files
In python, we use csv.reader() module to read the csv file. Here, we will show you how
to read different types of csv files with different delimiter
like quotes(""), pipe(|) and comma(,).
Normal CSV file
We have a csv file called people.csv having default delimiter comma(,) with following
data:
1, John, Washington
3, Brad, Texas
In the above program, we read the people.csv file. Then, we print the row of each
columns.
1, John, Washington
3, Brad, Texas
Quotes
What we think we become
Never regret anything that made you smile
Be yourself everyone else is already taken
In the above program, we register a dialect with name myDialect. Then, we
used csv.QUOTE_ALL to display all the characters after double quotes.
"pencil"|"eraser"|"sharpner"
"book"|"chair"|"table"
"apple"|"mango"|"grapes"
{'SN': '1', ' Name': ' John', ' City': ' Washington'}
{'SN': '2', ' Name': ' Eric', ' City': ' Los Angeles'}
{'SN': '3', ' Name': ' Brad', ' City': ' Texas'}
In above program, we use DictReader() to read people.csv file and map into a
dictionary. Then, we use dict() to print the data in dictionary format without order.
If we remove dict() function from the above program and only used print(row), output
will be
OrderedDict([('SN', '1'), (' Name', ' John'), (' City', ' Washington')])
OrderedDict([('SN', '2'), (' Name', ' Eric'), (' City', ' Los Angeles')])
OrderedDict([('SN', '3'), (' Name', ' Brad'), (' City', ' Texas')])
We can also register new dialects and use it in the DictReader() methods. Suppose we
have a people_data.csv in the following format
1| John| Washington
3| Brad| Texas
In Python we use csv.writer() module to write data into csv files. This module is similar
to the csv.reader() module.
Writing on Existing File
We have a people.csv file with following data.
1, John, Washington
3, Brad, Texas
In the above program, we modified the third row of people.csv and saved the result. At
first, we read the people.csv file using csv.reader() function. Then, we
used list() function to convert all the csv data in a list and store in lines. After that, we
changed third row of csv file with row i.e lines[2] = row. Finally, we write the values
of lines list to people.csv file.
Sometimes, we may need to add new rows in the existing csv file. So, we are going to
append a new row to people.csv file used in Example 1.
In the above program, we append a new row into people.csv. For this, we opened the
csv file in 'a' append mode. Then, we write the value of row after the last line of
the people.csv file.
When we open the person.csv file with text editor, then it will show :
Person,Age
Peter,22
Jasmine,21
Sam,24
In the above program, we use csv.writer() function to write data from a list csvData into
a csv file person.csv.
Note: The writerow() method writes one row at a time. If you need to write all the data
at once you can use writerows() method.
In the above program, we register a dialect with delimiter as pipe(|). Then we write a
list into a csv file dob.csv.
Output:
"SN"|"Items"
"1"|"Pen"
"2"|"Book"
"3"|"Copy"
In the above program, we register a dialect called myDialect. Then we use delimiter
as pipe(|) and quotechar as doublequote '"'.
In the above program, we create a custom dialect called myDialect with pipe(|) as
delimiter. Then, use fieldnames as headings of each column in csv file. Finally, we use
a DictWriter() to write dictionary data into grade.csv file.