CSV file CS
CSV file CS
Each Records
consists of field CSV forComma
Separate by
commas Separate Values
(delimiter)
It is human readable, i.e. data is stored using
ASCII and UNICODE characters.
It can easily edited using any text editor such as
Notepad, Excel, XML, database etc.
It has cross language compatibility. It means, the
data can be shared from Python CSV file to a non-
Python environment.
Due to its simple format, manipulation is quite
faster.
Capable of storing large amount of data.
Preferred import import and export format
databases and spreadsheet.
CSV File Text Files
1. The records are shown 1. The contents of the file
in a tabular form are displayed in running
text or ay be in a specific
format.
2. The first row of the 2. There is no header
record defines the concept in a text file.
heading.
3. The extension of file is 3. The extension may be
always *.csv to distinguish *.txt, *.doc, etc.
from others.
CSV module provides two type of
objects:
reader- to read from the csv files.
writer- to write in to the csv files.
To import csv module in our
program, write the following
statement:
import csv
Open a csv file:
Syntax : <file object>=open(<Filename>, mode)
OR
with open(<Filename>, mode=<filemode>) as <file
object>
Example:
f=open(Student.csv, “w”)
OR
f=open(“Student.csv,’’w’)
Close a csv file: f.close()
New line argument specifies
how would Python handle new
line characters while working
with csv files, on different
operating system.
Different operating system store
EOL characters differently.
Symbol/ Char Meaning Operating
System
CR[\r] Carriage Macintosh
Return
LF[\n] Line Feed UNIX
CR[\r], LF[\n] Carriage MS-DOS,
Return Windows
Line Feed
Null [\0] Null character Other
Operating
Additional optional argument
as newline=‘’ (null string no
space in between) with file
open() will ensure that no
translation of End of line
(EOL) character takes place.
csv.writer Returns a writer
object which writes
data into csv files.