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

Worksheet Topic: Data File Handling in Python CSV Files

Uploaded by

mjain1305
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)
50 views4 pages

Worksheet Topic: Data File Handling in Python CSV Files

Uploaded by

mjain1305
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

Worksheet

Topic: Data File Handling in Python

CSV Files
1. a) Write a program to read and print all the records present in a csv file ‘stu.csv’.
b) Write a program to count the number of records present in the above csv file
excluding the header.
2. What is the difference between writerow() and writerows() functions of the cs v module?
3. a) Write a program to write data to a file ‘data.csv’ with headers [STUID, NAME, MARKS].
Enter the record of five students to the file.
b) Write a program to search for the record of a particular student from the above csv file based on
inputted name.
c) Write a program to search for the record of the students who scored more than 90.
4. Vedansh is a Python programmer, for the Annual Sports Event he has created a csv file
named Result.csv, to store the results of students in different sports events.
The structure of the file Result.csv is:
[St_Id, St_Name, Game_Name, Result]
where
St_Id is Student ID (integer)
ST_name is Student Name (string)
Game_Name is name of game in which student is participating(string)
Result is result of the game whose value can be either 'Won', 'Lost' or 'Tie'

For efficiently maintaining data of the event, Vedansh wants to write the following user
defined functions:
Accept() – to accept a record from the user and add it to the file Result.csv. The column
headings should also be added on top of the csv file.
wonCount() – to count the number of students who have won any event.
As a Python expert, help him complete the task.
5. i) Give any one point of difference between a binary file and a csv file.
ii) Write a Program in Python that defines and calls the following user defined functions:
a) add() – To accept and add data of an employee to a CSV file ‘furdata.csv’. Each record
consists of a list with field elements as [fid, fname and fprice ] to store furniture id,
furniture name and furniture price respectively.
b) search()- To display the records of the furniture whose price is more than 10000.
Solutions
1. a)
import csv
f=open('csv1.csv')
c=csv.reader(f)
for i in c:
print(i)
f.close()

b)
import csv
f=open('csv1.csv')
c=csv.reader(f)
C=0
for i in c:
if c.line_num==1:
continue
else:
C=C+1
print("The number of records is ",C)
f.close()

import csv
f=open('data.csv', 'w')
w=csv.writer(f, delimiter=',')
L=['NAME','CLASS','MARKS']
w.writerow(L)
L2=[['AMIT',12,89],['SARA',11,78],['ZOYA',12,90], ['RAM',11,70], ['ZOYA',12,77]]
for i in L2:
w.writerow(i)
f.close()
OR

# by using writerows
import csv
f=open('data.csv', 'w')
w=csv.writer(f, delimiter=',')
L=['NAME','CLASS','MARKS']
w.writerow(L)
L2=[['AMIT',12,89],['SARA',11,78],['ZOYA',12,90], ['RAM',11,70], ['ZOYA',12,77]]
w.writerows(L2)
f.close()
4.
def Accept():
sid=int (input ("Enter Student ID "))
sname=input ("Enter Student Name ")
game =input ("Enter name of game ")
res=input ("Enter Result")
headings=["Student ID", "Student Name", " Game Name", "Result"]
data=[sid, sname, game, res]
f=open('Result.csv', 'a')
csvwriter=csv.writer (f)
csvwriter.writerow (headings)
csvwriter.writerow(data)
f.close()
def wonCount():
f=open('Result.csv', 'r')
csvreader=csv.reader (f, delimiter=', ')
for x in csvreader:
if x[3]=="WON":
print (x)
f.close()

You might also like