Solved QBank_CSV Files
Solved QBank_CSV Files
CLASS XII
CBSE SOLVED QUESTION BANK
CSV FILES
1 mark
3) Which of the following is the default character for the newline parameter for a csv
file object opened in write mode in Python IDLE ?
a. \n b. \t c. , d. ;
Ans: a. In Python IDLE, \n is used for the newline parameter. Hence, the correct option
is ‘a’.
4) 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 ( )
Ans: c. reader( ) and writer( ) is used to read the content of a csv file into an object.
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”
#CSV File Content
ENO, NAME, DEPARTMENT
E1, ROSHAN SHARMA, ACCOUNTS
E2, AASMA KHALID, PRODUCTION
E3, AMRIK GILL, MARKETING
E4, SARAH WILLIAMS, HUMAN RESOURCE
5) Nisha gets 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
Ans: In Statement – 1, csv will be in lowercase, Hence, the correct option is ‘b’.
6) 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”
Ans: According to given statements, we have to read the csv file named “Employee.csv”
and ‘r’ mode is used to read a file. Hence, the correct option is ‘a’.
7) Choose the function name ( with parameter ) that should be used in the line marked
as Statement – 3.
a. reader ( File ) b. readrows ( File )
c. writer ( File ) d. writerows( File )
Ans: The Correct syntax to read a csv file is csv.reader( File ). Hence, the correct option
is ‘a’.
8) 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 :
Ans: The correct statement is for R in ER : Hence, the Correct option is ‘a’.
9) Identify the suitable code for blank space in Statement-5 to match every row's 3rd
property with "ACCOUNTS".
a. ER [ 3 ] b. R [ 2 ]
c. ER [ 2 ] d. R [ 3 ]
Ans. 3rd property with “ACCOUNTS” mean with second index of R, because indexing
start with 0 (zero). Hence, the correct option is ‘b’.
10) 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 ]
Ans. Employee's Name and corresponding Department are in index of 1 and 2 as index
start with 0 (zero) in the given CSV file. Hence, the correct option is ‘c’.
11)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.
Ans: (a) Both A and R are true and R is the correct explanation for A
11) What is the advantage of using a csv file for permanent storage?
Ans:
Advantage of a csv file:
It is human readable – can be opened in Excel and Notepad applications
It is just like text file
12) Give any one point of difference between a binary file and a csv file.
Ans: Difference between binary file and csv file: (Any one difference may be
given)
Binary file:
Extension is .dat
Not human readable
Stores data in the form of 0s and 1s
CSV file
Extension is .csv
Human readable
Stores data like a text file
13) 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.
Ans: (a) Both A and R are true and R is the correct explanation for A
15) Which of the following is not a function / method of csv module in Python?
a. read() b. reader() c. writer() d. writerow()
Ans: a. read()
3/ 4 marks
16) Write a program in Python that defines and calls the following user defined
functions:
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 cid, s_name, Source,
destination to store Courier ID, Sender name, Source and destination address
respectively.
COURIER_SEARCH(): Takes the destination as the input and displays all the courier
records going to that destination.
Ans: (i) Program for COURIER_ADD():
import csv
def COURIER_ADD():
cid = input("Enter Courier ID: ") # Collect data from the user
s_name = input("Enter Sender Name: ")
source = input("Enter Source Address: ")
destination = input("Enter Destination Address: ")
record = [cid, s_name, source, destination] # Data to be written to the CSV file
with open('courier.csv', mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow(record) # Append the record to the CSV file
print("Courier details added successfully!")
COURIER_ADD() # Call the function
17) 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 book ID, book name and publisher respectively.
ii) Search_Book(): Takes publisher name as input and counts and displays number of
books published by them.
Ans: (i) Program for Add_Book():
import csv
def Add_Book():
with open('Book.csv', mode='a', newline='') as file: # Open the file in append
mode
writer = csv.writer(file)
book_id = input("Enter Book ID: ") # Prompt user for book details
book_name = input("Enter Book Name: ")
publisher = input("Enter Publisher: ")
book_details =[book_id, book_name, publisher]
writer.writerow(book_details)
print("Book details added successfully.")
def Search_Book():
publisher_input = input("Enter the publisher name to search for: ")
f=open("Books,csv",'r'):
rb=csv.reader(f)
count=0
for row in rb:
if row[2]== publisher_input:
count +=1
print(count, "books published by ", publisher_input, "publisher")
f.close()
18) 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)
19) 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 ‘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.
b) COUNTR() – To count the number of records present in the CSV file named
‘record.csv’.
Ans:
20) 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.
Ans:
21)
(b) In which mode will she open the file to add data into the file in Line 2 ?
Ans: f=open(“Teachers.csv”, “a”)
(c) In which mode will she open the file to read the data from the file in Line 3 ?
Ans: f=open(“Teachers.csv”,’r’)
(d) File in the blank in Line 4 to read the data from a CSV file.
Ans: FileReader= csv.reader(f)
22) 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.
Ans:
23) Create a function maxsalary() in Python to read all the records from an already existing
file record.csv which stores the records of various employees working in a department.
Data is stored under various fields as shown below:
i. Identify the suitable code for blank space in the line marked as Statement-1.
a) csv file b) CSV c) csv d) cvs
Ans: c) csv
ii. 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"
Ans: b) "Student.csv","w
iii. 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)
Ans: c) writer(fh)
iv. Identify the suitable code for blank space in 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
Ans: d) roll_no,name,Class,section
v. Identify the suitable code for blank space in the line marked as Statement-5.
a) data b) record c) rec d) insert
Ans: c) rec
vi. 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()
Ans: c) writerows()