Professional Documents
Culture Documents
Table of Contents
Writing data into di erent types of CSV les
Writing on Existing File
Normal CSV File
CSV File with Quotes
CSV les with Custom Delimiters
CSV le with Lineterminator
CSV le with quotechars
Python provides an easy way to work with csv le. It has csv module to read and write
data in the csv le.
Here, we will learn how to write data into csv les in di erent formats with the help of
examples.
In Python we use csv.writer() module to write data into csv les. This module is similar
to the csv.reader() module.
import csv
readFile.close()
writeFile.close()
When we open the people.csv le with text editor, then it will show:
In the above program, we modi ed the third row of people.csv and saved the result. At
rst, we read the people.csv le 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
RECOMMENDED READINGS
of csv le with row i.e lines[2] = row . Finally, we write the values of lines list to
people.csv
TUTORIAL
le.
EXAMPLES
Sometimes, we may need to add new rows in the existing csv le. So, we are going to
append a new row to people.csv le used in Example 1.
import csv
csvFile.close()
In the above program, we append a new row into people.csv . For this, we opened the
csv le in 'a' append mode. Then, we write the value of row after the last line of the
people.csv le.
import csv
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 le 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.
import csv
csv.register_dialect('myDialect',
quoting=csv.QUOTE_ALL,
skipinitialspace=True)
f.close()
"SN","Person","DOB"
"1","John","18/1/1997"
"2","Marie","19/2/1998"
RECOMMENDED READINGS
"3","Simon","20/3/1999"
"4","Erik","21/4/2000"
TUTORIAL EXAMPLES
"5","Ana","22/5/2001"
We can write csv le having custom delimiter by registering a new dialect with the help
of csv.register_dialect() .
import csv
csv.register_dialect('myDialect',
delimiter = '|',
quoting=csv.QUOTE_NONE,
skipinitialspace=True)
f.close()
RECOMMENDED READINGS
When TUTORIAL
we open dob.csv
EXAMPLES
le, we get following output:
SN|Person|DOB
1|John|18/1/1997
2|Marie|19/2/1998
3|Simon|20/3/1999
4|Erik|21/4/2000
5|Ana|22/5/2001
In the above program, we register a dialect with delimiter as pipe(|) . Then we write a
list into a csv le dob.csv .
We can write csv le with a lineterminator in Python by registering new dialects using
csv.register_dialect() class of csv module
import csv
f.close()
Fruit|Quantity
Apple|5
Orange|7
Mango|8
In above code, we register a new dialects as myDialect . Then, we use delimiter='|' where
a | is considered as column separator. After that, we use lineterminator='\r\n\r\n' where
RECOMMENDED READINGS
each row separates after every two lines.
TUTORIAL EXAMPLES
Note : Python's CSV module only accepts \r\n , \n or \r as lineterminator.
import csv
csv.register_dialect('myDialect',
delimiter = '|',
quotechar = '"',
quoting=csv.QUOTE_ALL,
skipinitialspace=True)
print("writing completed")
csvFile.close()
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 '"' .
Example 8: WritingREADINGS
RECOMMENDED dictionary into peak.csv le
import
TUTORIAL
csv EXAMPLES
data = [{'mountain' : 'Everest', 'height': '8848'},
{'mountain' : 'K2 ', 'height': '8611'},
{'mountain' : 'Kanchenjunga', 'height': '8586'}]
print("writing completed")
csvFile.close()
mountain,height
Everest,8848
K2,8611
Kangchenjunga,8586
In the above program, we use eldnames as headings of each column in csv le. Then,
we use a DictWriter() to write dictionary data into peak.csv le.
import csv
print("writing completed")
"Name"|"Grade"
"Alex"|"B"
"Bin"|"A"
"Tom"|"C"
RECOMMENDED READINGS
In the above program, we create a custom dialect called myDialect with pipe(|) as
Then, use eldnames as headings of each column in csv
delimiter.
TUTORIAL EXAMPLES
le. Finally, we use
a
DictWriter() to write dictionary data into grade.csv le.
Python Tutorial
Python Introduction
Python Flow Control
Python Functions
Python Datatypes
Python Files
Python Object & Class
Python Advanced Topics
Python Date and time
RECOMMENDED READINGS
Get Latest Updates on Programiz
TUTORIAL EXAMPLES
Enter Your Email
Subscribe
TUTORIALS
Python Tutorials
C Tutorials
Java Tutorials
Kotlin Tutorials
C++ Tutorials
Swift Tutorials
R Tutorials
Algorithms Tutorials
EXAMPLES
Python Examples
C Examples
Java Examples
Kotlin Examples
C++ Examples
R Examples
COMPANY
About
Advertising
Contact
LEGAL
Privacy Policy