Csvfiles 1 221013160753 7816639c
Csvfiles 1 221013160753 7816639c
Csvfiles 1 221013160753 7816639c
CSV is just like a text files , in a human readable format which is extensively used to
store tabular data , in a spreadsheet or database.
The separator character of CSV files is called a delimiters. Default delimiter is comma (,)
.Other delimiters are tab (‘\t’) , colon(:) , pipe (I) , semicolon (;) characters.
Easier to create.
Preferred import and export format for databases and spreadsheets (Tabular Form).
Capable of storing large amount of data (Tabular Form).
CSV is considered to be standard format.
CSV is faster to handle.
2
Python csv Module
import csv
f=open(“stu.csv”, “w”)
OR
f= open(“stu.csv”, “r”)
3
Closing csv Files
Close a csv file :
f.close( )
Newline argument specifies how would python handle new line characters while working
with csv files on different Operating systems.
4
EOL characters used in different Operating Systems
Note :
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.
5
Writing in csv files
csv.writer Returns a writer object which writes data into csv files.
<WriterObject>.writerow( ) Writes one row of data on to the writer object.
<WriterObject>.writerows( ) Writes multiple rows on to the writer object.
The csv.writer( ) function returns a writer object that converts the user’s data into a
delimited string. This string can later be used to write into CSV files using the writerow( )
function or writerows( ) function.
6
Program : 1 , Writing in a csv file by using writerow( ) function.
7
Output
8
Inside File [In Excel] - (By using newline argument in program)
9
Inside File [In Excel] - (Without using newline argument in program)
10
Inside File [In Notepad] - (By using newline argument in program)
11
Program : 2 , Writing in a csv file by using writerow( ) function and using semicolon as delimiter.
12
Output
13
Inside File [In Notepad] - (Without using newline argument in program) Semicolon
14
Inside File [In Excel] - (Without using newline argument in program) Semicolon
15
Program : 3 , Writing in a csv file by using writerows( ) function.
16
Output
17
Inside File [In Excel] - (By using newline argument in program)
18
Reading from CSV files
To read data from a CSV file , reader function of csv module is used.
It loads the data from CSV file into an iterable after parsing delimited
data.
19
How to read data from a CSV file ?
def read( ):
F=open(“Student.csv”, “r”,newline=’\r\n’)
Sreader=csv.reader(f)
for i in Sreader:
print(i)
F.close( )
20
Program : 4 , Reading data from CSV file without using newline argument.
21
Output
22
Inside File [In Excel] - (Without using newline argument in program)
23
Inside File [In Notepad] - (Without using newline argument in program)
24
Program : 5 , Reading data from CSV file by using newline argument.
25
Output
26
Inside File [In Excel] - (By using newline argument in program)
27
Inside File [In Notepad] - (By using newline argument in program)
28