CSV Files Worksheet
CSV Files Worksheet
Incomplete Code
import _______ #Statement-1
fh=open(______, _____, newline=’’) #Statement-2
stuwrite=csv. _______ #Statement-3
data=[ ]
header=[‘ROLL_NO’, ‘NAME’, ‘CLASS’, ‘SECTION’]
data.append(header)
for i in range(5):
roll_no=int(input(“Enter Roll Number : “))
name=input(“Enter Name : “)
Class=input(Enter Class : “)
rec=[ ______ ]
data.append(______) #Statement-4
stuwriter. _______ (data) #Statement-5
fh.close( ) #Statement-6
1. Identify the suitable code for blank space in the line marked as Statement-1/
a) csv file
b) CSV
c) csv
d) cvs
2. Identify the missing code for blank space in line marked as Statement-2
a) “Student.csv”, “wb”
b) “Student.csv”, “w”
c) “Student.csv”, “r”
d) “Student.cvs”, “r”
3. Choose the function name (with argument) that should be used in the blank space of line
marked as Statement-3
a) reader(fh)
b) reader(MyFile)
c) writer(fh)
d) writer(MyFile)
4. Identify the suitable code for blank space in the line marked as Statement-4.
a) ‘ROLL_NO’, ‘NAME’, ‘CLASS’, ‘SECTION’
b) ROLL_NO, NAME, CLASS, SECTION
c) ‘roll_no’, ‘name’, ‘class’, ‘section’
d) roll_no, name, class, section
5. Identify the suitable code for blank space marked as Statement-5.
a) data
b) record
c) rec
d) insert
6. Choose the function name that should be used in the blank space of line marked as Statement-
6 to create the desired CSV File.
a) dump()
b) load()
c) writerows()
d) writerow()
6. Ranjan Kumar of class 12 is writing a program to create a CSV file “user.csv” which will contain user
name and password for some entries. He has written the following code. As a programmer, help him to
successfully execute the given task.
addCsvFile(“Arjun”,”123@456”)
addCsvFile(“Arunima”,”aru@nima”)
addCsvFile(“Frieda”,”myname@FRD”)
readCsvFile() #Line 5
(a) Name the module he should import in Line 1.
(b) In which mode, Ranjan should open the file to add data into the file?
(c) Fill in the blank in Line 3 to read the data from a csv file.
(d) Fill in the blank in Line 4 to close the file.
(e) Write the output he will obtain while executing Line 5.
Sample Paper 2022-23
7. Assertion (A): CSV (Comma Separated Values) is a file format for data storage which looks like a text
file. Reason (R): The information is organized with one record on each line and each field is separated
by comma.
(a) Both A and R are true and R is the correct explanation for A.
(b) Both A and R are true and R is not the correct explanation for A.
(c) A is True but R is False.
(d) A is false but R is True.
8. What is the advantage of using a csv file for permanent storage?
Write a Program in Python that defines and calls the following user defined functions:
(i) ADD() – To accept and add data of an employee to a CSV file ‘record.csv’. Each record consists of
a list with field elements as empid, name and mobile to store employee id, employee name and
employee salary respectively.
(ii) COUNTR() – To count the number of records present in the CSV file named ‘record.csv’.
OR
Give any one point of difference between a binary file and a csv file.
Write a Program in Python that defines and calls the following user defined functions:
(i) 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.
(ii) search()- To display the records of the furniture whose price is more than 10000.
Count_Device() : to count and display number of peripheral devices whose price is less than 1000.
3. To specify a different delimiter while writing into a CSV file, _______ argument is used with writer().
4. The CSV file can take only comma as delimiter, (True/False).
5. _______ text format is accessible to all applications across several platforms.
6. _________ statement is used for importing csv module in Python.
Write Python function DISPEMP() to read the content of file emp.csv and display only those records
where salary is 4000 or above
Ans. import csv
def DISPEMP():
csvfile=open('emp.csv')
myreader = csv.reader(csvfile,delimiter='')
print("Employees whose salary is less than 4000")
for row in myreader:
if int(row[2])>4000:
print(row[0], ",", row[1], ",", row[2])
csvfile.close()
17. Consider the following CSV file (emp.csv):
Write a Python function DISPEMP() to read the content of file emp.csv and count how many employee
are earning less than 5000
Ans. import csv
def DISPEMP():
csvfile=open('emp.csv')
myreader = csv.reader(csvfile,delimiter='')
count=0
for row in myreader:
if int(row[2])<5000:
count+=1
print("NUMBER OF EMPLOYEE GETTING SALARY <5000 :",count)
csvfile.close()
18. Consider the following CSV file (emp.csv):
Write a Python function SNAMES() to read the content of file emp.csv and display the employee record
whose name begins from „S‟ also show no. of employee with first letter „S‟ out of total record. Output
should be: 2,Scott,4000 5,Sam,4200
Number of “S‟ names are 2/5
Ans. import csv
def SNAMES():
csvfile=open('emp.csv')
myreader = csv.reader(csvfile,delimiter='')
count_rec=count_s=0
for row in myreader:
if row[1][0].lower()=='s':
print(row[0],',',row[1],',',row[2])
count_s+=1
count_rec+=1
print("Number of 'S' names are ",count_s,"/",count_rec)
csvfile.close()
19. Write a python function CSVCOPY() to take sourcefile, targetfile as parameter and create a targetfile
and copy the contents of sourcefile to targetfile
Ans. import csv
def CSVCOPY(sourcefile,targetfile):
F1=open(sourcefile, ‘r’)
F2 = open(targetfile,'w')
mywriter=csv.writer(F2,delimiter=',')
myreader = csv.reader(csvfile,delimiter='')
for row in myreader:
mywriter.writerow([row[0],row[1],row[2]])
F1.close()
F2.close()