1| 13- 12- 202 1 / P R E P A R E D B Y : M r . J a g d i s h S P a t i l
INDIAN SCHOOL AL WADI AL KABIR
Class: XII Comp.
Sci.
Department: Computer Science
Date of submission:
20/12/2021
Worksheet No: 8
Topic: File Handling CSV Files
Note:
Section A
Fill in the blanks
[1] A _________ is a file format which stores records separated by comma.
.CSV
[2] One row of CSV file can be considered as _______ in terms of database.
record
[3] The CSV files can be operated by __________ and ____________ software.
Text Editor, Spreadsheet or Notepad, MS Excel
[4] The writerow() function is a part of _________ module.
CSV
[5] A _____ function allows to write a single record into each row in CSV file.
writerow()
[6] The _________ parameter of csv.reader() function is used to set a specific
delimiter like a single quote or double quote or space or any other character.
dialect
[7] A ________ is a parameter of csv.reader() function that accpets the keyword
arguments.
**fmtparams
[8] When you read csv file using csv.reader() function it returns the values in
_______ object.
nested list
[9] A ________ parameter is used to quote all fields of csv files.
quoting
[10] You can specify a quote character using _______ through writer function.
quotechar
2| 13- 12- 202 1 / P R E P A R E D B Y : M r . J a g d i s h S P a t i l
[11] The ____________ parameter instructs writer objects to only quote those
fields which contain special characters such as delimiter, quotechar or any of the
characters in lineterminator.
csv.QUOTE_MINIMAL
[12] To avoid quote fields in csv.writer() function, use _________ parameter.
csv.QUOTE_NONE
[13] If you want to change a default delimiter of csv file, you can specify ________
parameter.
delimiter
[14] CSV module allows to write multiple rows using ____________ function.
writerrows()
[15] ___________ instances or objects return by the writer function.
writer()
Section B
True/False CSV in Python class 12
[1] Each row read from the csv file is returned as a list of strings.
True
[2] You can import csv module functions in following manner: from csv import
writerow, reader
True
[3] The csv.QUOTE_NONNUMERIC is used to quotes all kind of data.
False
[4] When csv.QUOTE_NONE is used with writer objects you have to specify the
escapechar option parameter to writerow() function.
True
[5] You cannot change the by default comma as a value separater.
False
[6] The quotechar function must be given any type of character to separate
values.
True
[7] The default line terminator is \n in csv file.
True
[8] The write row function creates header row in csv file by default.
False
3| 13- 12- 202 1 / P R E P A R E D B Y : M r . J a g d i s h S P a t i l
[9] You cannot insert multiple rows in csv file using python csv module.
False
[10] In csv file, user can insert text values and date values with single quote like
MySQL.
True
Section C
MCQs/One word Answer Questions CSV in Python class 12
1. Expand: CSV
Comma Separated Value
2. Which of the following module is required to import to work with CSV file?
1. File
2. CSV
3. pandas
4. numpy
3. Which of the following is not a function of csv module?
0. readline()
1. writerow()
2. reader()
3. writer()
4. The writer() function has how many mandatory parameters?
0. 1
1. 2
2. 3
3. 4
5. Name the function which used to write a row at a time into CSV file.
writerow()
6. Which of the following parameter needs to be added with open function to
avoid blank row followed file each row in CSV file?
0. qoutechar
1. quoting
2. newline
3. skiprow
7. Anshuman wants to separate the values by a $ sign. Suggest to him a pair of
function and parameter to use it.
0. open,quotechar
1. writer,quotechar
2. open,delimiter
3. writer, delimiter
4| 13- 12- 202 1 / P R E P A R E D B Y : M r . J a g d i s h S P a t i l
8. Which of the following is tasks cannot be done or difficult with CSV
module?
0. Data in tabular form
1. Uniqueness of data
2. Saving data permanently
3. All of these
9. Which of the following is by default quoting parameter value?
0. csv.QUOTE_MINIMAL
1. csv.QUOTE_ALL
2. csv.QUOTE_NONNUMERIC
3. csv.QUOTE_NONE
10. Which of the following is must be needed when csv.QUOTE_NONE
parameter is used?
0. escapechar
1. quotechar
2. quoting
3. None of these
Section D
Descriptive Questions CSV in python class 12
[1] Write the functions required to handle CSV files.
To handle CSV files following function required:
1. Open()
2. reader()
3. writer()
4. writerow()
5. close()
[2] Write to ways to import a csv module.
1. import csv
2. from csv import *
[3] Explain following functions with example.
1. reader()
2. writer()
3. writerow()
Section E
Case study based questions CSV in Python class 12
[1] Write python code to create a header row for CSV file “students.csv”. The
column names are : Adm.No, StudentName, City, Remarks
5| 13- 12- 202 1 / P R E P A R E D B Y : M r . J a g d i s h S P a t i l
from csv import writer
f = open("students.csv","w")
dt = writer(f)
dt.writerow(['Admno','StudentName','City','Remarks'])
f.close()
[2] Observe the following code and fill in the given blanks:
import csv
with _________ as f:
#1
r = csv.______(f)
#2
for row in ______:
#3
print(_____) #4
1. open(“data.csv”)
2. reader
3. r
4. row
[3] Write steps to print data from csv file in list object and support your answer
with example.
1. Import csv module import csv
2. Open the csv file in reading mode f = open(“demo.csv”,”r”)
3. Use list object to store the data read from csv using reader data =
csv.reader(f)
4. close the csv file f.close()
5. print the data object print(data)
[4] How to print following data for cust.csv in tabular form usig python code?
SNo
Customer Name
City
1
Dhaval
Anand
2
Anuj
Ahmedabad
3
Mohan
Vadodara
6| 13- 12- 202 1 / P R E P A R E D B Y : M r . J a g d i s h S P a t i l
4
Sohan
Surat
from csv import reader
f = open("cust.csv","r")
dt = reader(f)
data = list(dt)
f.close()
for i in data:
for j in i:
print('\t|',j,end=" ")
print()
[5] Write code to insert multiple rows in the above csv file.
from csv import writer
with open("cust.csv","a",newline="\n")
as f:
dt = writer(f)
while True:
sno= int(input("Enter Serial No:"))
cust_name = input("Enter customer name:")
city = input("Enter city:")
amt = int(input("Enter amount:"))
dt.writerow([sno, cust_name, city, amt])
print("Record has been added.")
print("Want to add more record?Type YES!!!")
ch = input()
ch = ch.upper()
if ch=="YES":
print("*************************")
else:
break
[6] Write code to delete a row from csv file.
import csv
record = list()
custname= input("Please enter a customer name to delete:")
with open('cust.csv', 'r') as f:
data = csv.reader(f)
for row in data:
record.append(row)
7| 13- 12- 202 1 / P R E P A R E D B Y : M r . J a g d i s h S P a t i l
for field in row:
if field == custname:
record.remove(row)
with open('cust.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(record)