0% found this document useful (0 votes)
75 views7 pages

CSV Files Worksheet

Uploaded by

nazhaqt
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)
75 views7 pages

CSV Files Worksheet

Uploaded by

nazhaqt
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/ 7

CSV Files Worksheet

Sample Paper 2021-22


1. Which of the following character acts as a delimiter in a csv file?
a. (colon) : b. (hyphen) - c. (comma) , d. (vertical line) |
2. Syntax for opening Student.csv file in write mode is
myfile=open(“Student.csv”, “w”, newline=’’)
what is the importance of newline=’’?
a. A newline gets added to the file.
b. Empty string gets appended to the first line.
c. Empty string gets appended to all lines.
d. EOL translation is suppressed.
3. What is the correct expansion of CSV files?
a. Comma Separable Values.
b. Comma Separated Values
c. Comma Split Values.
d. Comma Separation Values.
4. Which of the following is not a function/method of csv module in Python?
a. read()
b. reader()
c. writer()
d. writerow()
5. Rohit, a student of class 12, is learning CSV file module in Python. During examination, he has been
assigned an incomplete python code (shown below) to create a CSV File ‘ Student.csv’ (content shown
below). Help him in completing the code which creates the desired CSV File.

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.

import _____________ # Line 1


def addCsvFile(UserName,PassWord): # to write / add data into the CSV file
f=open(' user.csv','________') # Line 2
newFileWriter = csv.writer(f)
newFileWriter.writerow([UserName,PassWord])
f.close()

#csv file reading code


def readCsvFile(): # to read data from CSV file
with open(' user.csv','r') as newFile:
newFileReader = csv._________(newFile) # Line 3
for row in newFileReader:
print (row[0],row[1])
newFile.______________ # Line 4

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.

Board Paper 2022 (Term 1)


9. Which of the following function is used with the csv module in Python to read the contents of a csv
file into an object?
a)readrow() b)readrows() c)reader() d)load()
10. Nisha , an intern in ABC Pvt Ltd, is developing a project using the csv module in Python. She has partially
developed the code as follows leaving out statements about which she is not very confident. The code
also contains errors in certain statements. Help her in completing the code to read the desired CSV File
named ‘Employee.csv”

#incomplete code with errors


import csv #statement1
with open (_____, _______, newline=’’) as File: #Statement 2
ER = csv. ________ #Statement 3
for R in range(ER): #Statement 4
if _____ == ‘ACCOUNTS’: #Statement 5
print( ______ , ______) #Statement 6
(i) Nisha get an error for the module name used in Statement-1. What should she write in place
of CSV to import the correct module?
a)file b)csv c)Csv d)pickle
(ii) Identify the missing code for blank spaces in the line marked as Statement-2 to open the
mentioned file.
a)”Employee.csv”, “r” b)”Employee.csv”,”w”
c)”Employee.csv”,”rb” d)”Employee.csv”,”wb”
(iii) Nisha gets an error in Statement 4. What should she write to correct the statement?
a) for R in ER: b) while R in range (ER):
c) for R = ER: d) while R = ER:
(iv) Identify the suitable code for blank space in Statement-5 to match every roe’s 3rd property
with “ACCOUNTS”.
(a) ER[3] (b) R[2]
(c) ER[2] (D) R[3]
(v) Identify the suitable code for blank space in Statement-6 to display every Employee’s Name
and corresponding Department?
(a) ER[1],R[2] (b)R[1],ER[2]
(c) R[1],R[2] (d)ER[1], ER[2]
BOARD PAPER 2022-23
11. a) Write one difference between CSV and text files.
Write a program in python that defines and calls the following user -defined functions:
1. COURIER_ADD :It takes the values from the user and adds the details to a csv file ‘courier.csv’ .
Each record consists of a list with field elements as a cid, s_name, source and destination to
store courierID, Sender name, Source and destination address respectively.
2. COURIER_SEARCH() : Takes the destination as the input and displays all the courier records
going to that destination.
OR
b) Why it is important to close a file before exiting?
Write a program in Python that defines and calls the following user- defined functions:
i) Add_Book() : Takes the details of the books and adds them to a csv file ‘Book.csv’. Each record
consists of a list with field elements as book_ID, B_name and pub to store bookID, bookName
and publisher respectively.
ii) Search_Book() :Takes publisher name as input and counts and displays the number of books
published by them.
Sample Paper 2023-24
12. Vedansh is a Python programmer working in a school. 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
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
BOARD PAPER 2023-24
13. Assertion (A) : CSV file is a human readable text file where each line has a number of fields, separated
by comma or some other delimiter.
Reason (R) : writerow() method is used to write a single row in a CSV file.
(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.
14. Sangeeta is a Python programmer working in a computer hardware company. She has to maintain the
records of the peripheral devices. She created a csv file Peipheral.csv, to store the details. The structure
of Peripheral.csv is:
[P_id, P_name, Price]
where
P_id is Peripheral device ID (integer)

P_name is Peripheral device name (String)

Price is Peripheral device price (integer)

Sangeeta wants to write the following user defined functions:


Add_Device() : to accept a record from the user and add it to a csv file, Peripheral.csv.

Count_Device() : to count and display number of peripheral devices whose price is less than 1000.

Sample Paper 2024-25


15. A csv file "Happiness.csv" contains the data of a survey. Each record of the file contains the following
data:
 Name of a country
 Population of the country
 Sample Size (Number of persons who participated in the survey in that country)
 Happy (Number of persons who accepted that they were Happy)
For example, a sample record of the file may be:
[‘Signiland’, 5673000, 5000, 3426]
Write the following Python functions to perform the specified operations on this file:
(I) Read all the data from the file in the form of a list and display all those records for which the
population is more than 5000000.
(II) Count the number of records in the file.
ADDITIONAL QUESTIONS
1. A CSV file is also known as a:
a) Flat file
b) 3D file
c) String file
d) Random file
2. Which module is provided by Python to do several operations on CSV file?

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.

7. Comma is the default delimiter for a CSV file (True/False).


8. To read data from a CSV file _______ function of CSV is used.
9. CSV files are opened with _________ argument to suppress EOL translation.
10. For reading from a CSV file ______ loads the data from the CSV file, parses it and makes it available in
the form of an iterator wherefrom the records can be fetched row by row.
11. Identify the error in the following code:
i) import csv
f=open(“attendees1.csv”)
csv_f=csv.writer(f)

ii) import csv


f=open(“Attendees.csv”)
csv_f=csv.reader()
12. Which of the following file types can be opened with notepad as well as ms excel?
a. Text Files
b. Binary Files
c. CSV Files
d. None of these
13. Which of the following function flushes the files implicitly?
a. flush()
b. close()
c. open()
d. fflush()
14. Which of the following functions flushes the data before closing the file?
a. flush()
b. close()
c. open()
d. fflush()
15. Write a program in Python that defines and calls the following user defined functions.
i) ADD() – to accept and add data to a CSV file “Furdata.csv”. each record consists of a list with field
elements FID, FName and FPrice to store furniture ID, Name and Price.
ii) SEARCH() – to display the records of the furniture whose price is more than 10000.
16. Consider the following CSV file (emp.csv):

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()

You might also like