0% found this document useful (0 votes)
15 views3 pages

Python CSV File Handling Worksheet

The document contains a worksheet focused on completing Python code related to reading and writing CSV files using the csv module. It includes exercises for reading from and writing to CSV files, handling user input, and searching records based on specific criteria. Additionally, it presents scenarios to analyze the output of CSV operations under different conditions.

Uploaded by

dhruvibarai809
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)
15 views3 pages

Python CSV File Handling Worksheet

The document contains a worksheet focused on completing Python code related to reading and writing CSV files using the csv module. It includes exercises for reading from and writing to CSV files, handling user input, and searching records based on specific criteria. Additionally, it presents scenarios to analyze the output of CSV operations under different conditions.

Uploaded by

dhruvibarai809
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

WORKSHEET - CSV FILES

1. Observe the following code to read and display the content of a csv file ‘[Link]’ and complete the missing
statements.

import csv
with open(______,'____',_________) as csvfile:
data=__________(csvfile)
for __________:
print(rec)
2. Observe the following code to write into a file ‘[Link]’ with tab delimited and complete the missing
statements.

import _____
mycsv=open(______. ______)
csvwriter=[Link]( ______, ________ )
csvwriter._________(['EmpID','Empname','Salary'])
rows=[[1001,'Ram',55000],[1002,'Doshi',70000],[1003,'Reshma',65000]]
________________________(rows)
[Link]()

2. Krishna of class 12 is writing a program to read the details of Sports performance and store in the csv file
“[Link]” delimited with a tab character. As a programmer, help him to achieve the task.
import ___________ # Line 1
f = open(“[Link]”,”a”)
wobj = csv.______________ (f, delimiter = „\t‟) # Line 2
[Link]( [„Sport‟, „Competitions‟, „Prizes Won‟] )
ans = “y‟
i=1
while ans == ‘y’:
print(“Record :”, i)
sport = input(“Sport Name :”)
comp = int(input(“No. of competitions participated :”))
prize = int(input(“Prizes won:”))
record = ____________________ # Line 3
wobj.______________ (rec) # Line 4
i += 1
ans = input(“Do u want to continue ? (y/n) :”)
f.___________ # Line 5

3. Rohit, a student of class 12th, is learning CSV File Module in Python. During examination, he has been
assigned an incomplete python code (shown below) to create a CSV File '[Link]' (content shown below).
Help him in completing the code which creates the desired CSV File.
CSV File
1,AKSHAY,XII,A
2,ABHISHEK,XII,A
3,ARVIND,XII,A
4,RAVI,XII,A
5,ASHISH,XII,A
Incomplete Code
import_____ #Statement-1
fh = open(_____, _____, newline='') #Statement-2
stuwriter = csv._____ #Statement-3
data = [] header = ['ROLL_NO', 'NAME', 'CLASS', 'SECTION']
[Link](header)
for i in range(5):
roll_no = int(input("Enter Roll Number : "))
name = input("Enter Name : ")
Class = input("Enter Class : ")
section = input("Enter Section : ")
rec = [_____] #Statement-4
[Link](rec)
stuwriter. _____ (data) #Statement-5
[Link]()
4. Amit, a student of class 12th is trying to write a program to search the record from “[Link]” according to the
admission number input from the user. Structure of record saved in “[Link]” is Adm_no, Name, Class,
Section, Marks. He has written the partial code and has missed out certain statements, You as an expert of
Python have to provide the answers of missing statements based on the following code of Amit.
import _____________________ #Statement1
f = open(__________________) #Statement2
d=csv._______________________(f) #Statement3
next(f) #To Skip Header Row
k=0
adm = int(input("Enter admission number"))
for row in d:
if int(row[0])_______________adm: #Statement4
print("Adm no = ", row[0])
print("Name = ", row[___________]) #Statement5
print("Class = ", row[2])
print("Section = ", row[3])
print("Marks = ", row[4])
break
_____________ : #Statement6
print("Record Not Found")
5. Observe the following code and find the correct output to be displayed.

import csv
mycsv=open('[Link]','w') # line 1
csvwriter=[Link](mycsv,delimiter=':')
[Link](['SID','SName','Marks'])
Data=[[101,'Ram',85],[102,'Doshi',92],[103,'Reshma',95]]
[Link](Data)
[Link]()
with open('[Link]','r') as fh:
reader=[Link](fh) #line 2
for rec in reader:
print(rec)
a) ['SID', 'SName', 'Marks'] b) ['SID', 'SName', 'Marks']
['101', 'Ram', '85'] []
['102', 'Doshi', '92'] ['101', 'Ram', '85']
['103', 'Reshma', '95'] []
['102', 'Doshi', '92']
[]
['103', 'Reshma', '95']
[]
c)) ['SID:SName:Marks'] d) ['SID:SName:Marks']
[] ['101:Ram:85']
['101:Ram:85'] ['102:Doshi:92']
[] ['103:Reshma:95']
['102:Doshi:92']
[]
['103:Reshma:95']
[]
6 Pick out the output for the above code ,if line 2 has been modified as below
reader=[Link](fh,delimiter=':')
7 Pick out the output for the above code ,if only line 1 has been modified as below
mycsv=open('[Link]','w',newline='')
8 Pick out the output for the above code ,if both line 1 and 2 has been modified as below
Line 1: mycsv=open('[Link]','w',newline='')
Line 2: reader=[Link](fh,delimiter=':')

You might also like