Complet Documnetation for Smart Assistant Application for Disabled Person
CSV Files-1.pdf
1. 1
CSV Files
CSV stands for Comma Separated Values.
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.
Main Features of CSV Files
CSV stands for Comma Separated Values.
It is used for storing tabular data in a spreadsheet or database.
Each record consists of fields separated by commas (delimiter).
Each line of a file is called a record.
2. 2
Advantages of CSV Files
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.
Disadvantages of CSV Files
Poor support of special characters.
No standard way to represent binary data.
Problems with importing CSV into SQL (no distinction between NULL and quotes.
CSV allows to move most basic data only.
There is no distinct between text and numeric values.
3. 3
Python csv Module
csv module provides two types 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
Opening csv Files
Open a csv file :
f=open(“stu.csv”, “w”)
OR
f= open(“stu.csv”, “r”)
4. 4
Closing csv Files
Close a csv file :
f.close( )
Role of Argument newline in Opening of csv files
Newline argument specifies how would python handle new line characters while working
with csv files on different Operating systems.
Different operating system store EOL characters differently.
5. 5
EOL characters used in different Operating Systems
Symbol / Char Meaning Operating System
CR [r] Carriage Return Macintosh
LF [n] Line Feed Unix
CR / LF [rn] Carriage Return / Line Feed MS-Dos, Windows
NULL [0] Null Character Other OS
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.
6. 6
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.
Role of 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.
7. 7
Program : 1 , Writing in a csv file by using writerow( ) function.
19. 19
Reading from CSV files
To read data from a CSV file , reader function of csv module is used.
csv.reader( ) Returns a reader object.
It loads the data from CSV file into an iterable after parsing delimited
data.
Steps to read data from a csv file.
1. Import csv module import csv
2. Open a CSV file in read mode Fh=open(“student.csv”, “r”)
3. Create the reader object Sreader=csv.reader(Fh)
4. Fetch data through for loop , row by row for rec in Sreader:
print(rec)
5. Close the file Fh.close( )
20. 20
How to read data from a CSV file ?
def read( ):
F=open(“Student.csv”, “r”,newline=’rn’)
Sreader=csv.reader(f)
for i in Sreader:
print(i)
F.close( )
21. 21
Program : 4 , Reading data from CSV file without using newline argument.