CVS FILE
‘The CSV (Comma Separated Values) is a special text file format in which rows of data are present
and the individual data elements are separated by commas. The CSV format is the most common
import and export format for spreadsheets and databases. The esv files have the extension .csv
Example for data contained in esv file
Raghu,23,100
Tisha,45,230
‘Yatin,67,90
Advantages of csv files:
+ CSV is human readable and easy to edit manually.
+ CSV is simple to implement and parse.
+ CSV is processed by almost all existing applications,
+ CSV is smaller in size
+ CSV is considered to be standard format
+ CSV is compact. For XML you start tag and end tag for each column in each row. In CSV
you write the column headers only once.
+ CSV can handle large unstructured data generated by social media effectively
Examples of some valid formats of CSV files are:
1, Each record is located on a separate line, delimited by a line break (CRLF). For example:
One, two,three CRLF
four,five,six CRLF
2, The last record in the file may or may not have an ending line break. For example:
One,two,three CRLF
four five.six
3. ‘There maybe an optional h
format as normal record lines.
ler line appearing as the first line of the file with the same
For example:
Name,Rollno,Marks CRLF € Header line
Raghu, 1011,56 CRLF
‘Swara,1012,78 CRLF
4, Each field may or may not be enclosed in double quotes. If fields are not enclosed with
double quotes, then double quotes may not appear inside the fields. For example:
“one”, "two", "three" CRLF
Eight,nine,ten
[Note:
1. CR stands for the character 'Carriage Return' with Integer ASCII code - 13 and
C++/Python notation \r or "yr".
2. LF stands for the character ‘Line Feed' with Integer ASCH code - 10 and C++/Python
notation \n or 'in’3._In Windows platform the Enter Key / End of Line(EOL) / newline character is represented
by the character combination CRLF i.e. 'v''in python.
4, In Unix/Linux platforms the Enter Key /End of Line (EOL) / newline character is
represented by the character LF only i.e. "a in python.
5. In Macintosh platforms the Enter Key / End of Line (EOL) / newline character is
represented by the character CR only i. 'v" in python.
6. When opening a file for reading CSV file add the parameter, newline='EOL_character' to
process the End of Line correctly.
While opening a CSV file for reading on Windows platform, add the parameter, newline="v\n’ in
the open() method to avoid adding an extra blank line while processing/displaying output on the
screen ]
In CSV files there can be delimiters other than comma(,)
Tab separated values
Raghu 23 100
Tisha 45 230
Yatin 6790
Semicolon separated values
Raghu ;23;100
Tisha;45;230
Yatin;67;90
Using CSV module to handle esv files
What is csv module?
The esv module implements classes to read and write tabular data in CSV format, The esv module
handles the different types of delimiters and quoting characters in CSV files and efficiently
manipulate such data, hiding the details of reading and writing the data from the programmer.
The module has to be imported in python program to handle esv files.
import esv as
or
import esv
READING FROM A CSV FILE-READER OBJECT
Reading from a esv file is done using a reader object. The CSV file is opened as a text
file with Python's built-in open() function, which returns a file object. This creates a special
type of object to access the CSV file (reader object), using the reader()
function.The reader object is an iterable that gives us access to each line of the CSV file as a
list of fields. You can also use next() directly on it to read the next line of the CSV file,
or you can treat it like a list in a for loop to read all the lines of the file (as lists of the
file’s fields).
Syntax:
csv.reader(fileobject,delimiter=" “)-> it returns a reader object
PROGRAM TO READ AND DISPLAY CONTE!
'S OF CSV FILE
#reading and displaying contents from a csv file
import esv as ¢
with open(‘12ACS.csv’,"r") as myesv:
esvreader=c.reader(myesv,delimiter=",")
for row in csvreader.
wrint (row)
CREATING A CSV FILE THROUGH A PYTHON PROGRAM FOR WRITING DATA
To write to a CSV file in Python, we can use the csv. writer() function. The
esv.vwriter() function retums 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.
In order to write to a CSV file, we create a special type of object to write to the CSV
file "writer object", which is defined in the CSV module, and which we create using,
the writer() funetion.
The writerow() method allows us to write a list of fields to the file. The fields
can be strings or numbers or both. Also, while using writerow(), you do not need to
add a new line character (or other EOL indicator) to indicate the end of the line,
writerow() does it for you as necessary.
PROGRAM TO CREATE A CSV FILE AND WRITE DATA IN IT
def createcsv()
fropen("cricket.csv","w" newline=")
s=esv.writer(fdelimiter=
sovriterow({"playername”, "runsscored", "Matchsplayed"))
while True:
c=eval(input("Enter the name ,runs, matches")
s.vriterow(c)
ch=input("Do you want to continue (Y/N)")
if ch.upper)!="¥"
break
feloseQ
writerow() Vs writerows()
writerow() is used to write a single row to a esv file.writerows() is used to write multiple rows/lines to esv file
To demonstrate writerow() and writerows()
import esv
fRopen("one.csv","w",newline=")
cesvwriter=csv.writer(fdelimiter=",)
cesvwriter. writerow([‘one','two', three'])
I= [(23,45,67).(34,56,78),(45,67,78)]
eswwriter.writerows())
ficlose()
OUTPUT:
one,two,three
23,45,67
34,56,78
45,67,78