0% found this document useful (0 votes)
16 views4 pages

CSV Files

The document provides an overview of CSV (Comma Separated Values) files, highlighting their structure, differences from TXT files, and access modes. It explains the use of the CSV module in Python for reading and writing data, including examples of writing and reading CSV files. Additionally, it discusses the importance of the newline argument when opening CSV files to prevent extra newlines in the data.

Uploaded by

Shaizal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

CSV Files

The document provides an overview of CSV (Comma Separated Values) files, highlighting their structure, differences from TXT files, and access modes. It explains the use of the CSV module in Python for reading and writing data, including examples of writing and reading CSV files. Additionally, it discusses the importance of the newline argument when opening CSV files to prevent extra newlines in the data.

Uploaded by

Shaizal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

FILE HANDLING

PART 3 : CSV FILES


CSV FILES
*Stands for comma separated values.
*Used to handle large amount of data
*Data is stored in the form of rows and columns / tabular form. So it is easy to
export from databases or spreadsheets.
*CSV files can be easily imported to other programs.
*Languages which support text files will also support csv files
*File extension is .csv

Are ‘csv’ files different from ‘txt’ files?


Yes
txt files contain text which can be opened by any text editor and there is plain
text with no format.
Csv files also contain text data but in a format where each line is considered
as row/record which has many fields. Fields are the values separated by a
delimiter like ‘,’ , ‘*’ , ‘/’ etc. the first record is the title of each field.

csv file access modes


r , w , a , r+ or +r , w+ or +w , a+ or +a
Role of csv module
import csv
csv module provides two types of objects:
i)reader – to read from the csv file.
ii)writer – to write into the csv file.
Role of argument newline in opening of csv files
Eg) F=open("Student.csv","w",newline='')
The additional argument newline ='' ensures that no translation of EOL
character takes place (ie to avoid any extra newlines in the data).
Note: Different operating systems store EOL character differently.
Writing into a csv file
csv.writer() It returns a writer object which writes data
into csv files.
<writer object>.writerow() Writes one row of data on to the writer
object.
<writer object>.writerows() Writes more than one row of data on to the
writer object.
Ms Maya Giby
PGT Computer Sc
The csv.writer() function returns a writer object that converts the user’s data
into a delimited string.
The string can later be used to write into CSV files using the writerow( )
function
Reading from a csv file
csv.reader() function is used to read the records from the csv file.
The records read are in the form of list of strings
i)writing into a csv file using writerow()

Case 1
#Writing into a csv file o/p
import csv Enter rollno:101
def WRITE(): Enter name:RAJ
F=open("Student1.csv","w") Enter marks:98
W=csv.writer(F) More records to enter(y/n):y
W.writerow(['rno','name','marks']) Enter rollno:102
while True: Enter name:RAVI
R=int(input("Enter rollno:")) Enter marks:89
N= input("Enter name:") More records to enter(y/n):n
M=int(input("Enter marks:"))
data=[R,N,M]
W.writerow(data)
ch=input("More records to enter(y/n):")
if ch =='n':
break
F.close()
WRITE()
#Reading a csv file o/p
def READ(): ['rno', 'name', 'marks']
F=open("Student1.csv","r") []
R =csv.reader(F) ['101', 'RAJ', '98']
for i in R: []
print(i) ['102', 'RAVI', '89']
F.close() []
READ()

Case 2
#Writing into a csv file o/p
import csv Enter rollno:101
def WRITE(): Enter name:RAJ
F=open("Student1.csv","w",newline='') Enter marks:98
……….. More records to enter(y/n):y
Ms Maya Giby
PGT Computer Sc
F.close() Enter rollno:102
WRITE() Enter name:RAVI
Enter marks:89
More records to enter(y/n):n
def READ(): o/p
F=open("Student1.csv","r") ['rno', 'name', 'marks']
R =csv.reader(F) ['101', 'RAJ', '98']
for i in R:
print(i)
['102', 'RAVI', '89']
F.close()
READ()

Case 3
#Writing into a csv file
import csv
def WRITE():
F=open("Student1.csv","w",newline='')
W=csv.writer(F, delimiter =';')
-------------
F.close()
WRITE()
def READ(): o/p
F=open("Student1.csv","r") ['rno;name;marks']
R =csv.reader(F) ['101;RAJ;98']
for i in R:
print(i)
['102;RAVI;89']
F.close()
READ()

Case 4
#Writing into a csv file o/p
import csv Enter rollno:101
def WRITE(): Enter name:RAJ
F=open("Student1.csv","w") Enter marks:98
……….. More records to enter(y/n):y
F.close() Enter rollno:102
WRITE() Enter name:RAVI
Enter marks:89
More records to enter(y/n):n
def READ(): o/p
F=open("Student1.csv","r",newline='\n') ['rno', 'name', 'marks']
R =csv.reader(F) ['101', 'RAJ', '98']
Ms Maya Giby
PGT Computer Sc
for i in R: ['102', 'RAVI', '89']
print(i)
F.close()

READ()

ii)writing into a csv file using writerows()


import csv Enter rollno:101
def WRITE(): Enter name:RAJ
F=open("Student1.csv","w",newline="") Enter marks:98
W=csv.writer(F) More records to enter(y/n):y
L=[] Enter rollno:102
L.append(['rno','name','marks']) Enter name:RAVI
while True: Enter marks:89
R=int(input("Enter rollno:")) More records to enter(y/n):n
N= input("Enter name:")
M=int(input("Enter marks:"))
data=[R,N,M]
L.append(data)
ch=input("More data to enter(y/n):")
if ch =='n':
break
W.writerows(L)
F.close()

def READ(): ['rno', 'name', 'marks']


F=open("Student1.csv","r") ['101', 'RAJ', '98']
R =csv.reader(F) ['102', 'RAVI', '89']
for i in R:
print(i)
F.close()
READ()

Ms Maya Giby
PGT Computer Sc

You might also like