CSV in Python class 12 offers assignments and questions answers on data file handling in python CBSE class 12 computer science.
Topics Covered
CSV in Python class 12 Questions and Answers
Here in we go for CSV in Python class 12 questions and answers.
Objective type questions
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
[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()
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
[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
MCQs/One word Answer Questions – CSV in Python class 12
- Expand: CSV
- Comma Separated Value
- Which of the following module is required to import to work with CSV file?
- File
- CSV
- pandas
- numpy
- Which of the following is not a function of csv module?
- readline()
- writerow()
- reader()
- writer()
- The writer() function has how many mandatory parameters?
- 1
- 2
- 3
- 4
- Name the function which used to write a row at a time into CSV file.
- writerow()
- Which of the following parameter needs to be added with open function to avoid blank row followed file each row in CSV file?
- qoutechar
- quoting
- newline
- skiprow
- Anshuman wants to separate the values by a $ sign. Suggest to him a pair of function and parameter to use it.
- open,quotechar
- writer,quotechar
- open,delimiter
- writer, delimiter
- Which of the following is tasks cannot be done or difficult with CSV module?
- Data in tabular form
- Uniqueness of data
- Saving data permanently
- All of these
- Which of the following is by default quoting parameter value?
- csv.QUOTE_MINIMAL
- csv.QUOTE_ALL
- csv.QUOTE_NONNUMERIC
- csv.QUOTE_NONE
- Which of the following is must be needed when csv.QUOTE_NONE parameter is used?
- escapechar
- quotechar
- quoting
- None of these
Descriptive Questions CSV in python class 12
[1] Write the functions required to handle CSV files.
To handle CSV files following function required:
- Open()
- reader()
- writer()
- writerow()
- close()
[2] Write to ways to import a csv module.
- import csv
- from csv import *
[3] Explain following functions with example.
- reader()
- writer()
- writerow()
Case study based questions – CSV in Python class 12
[4] Write python code to create a header row for CSV file “students.csv”. The column names are : Adm.No, StudentName, City, Remarks
Creating a header row is one of the most important aspects of CSV in python class 12. Use the following code to do so.
from csv import writer
f = open("students.csv","w")
dt = writer(f)
dt.writerow(['Admno','StudentName','City','Remarks'])
f.close()
[5] 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
- open(“data.csv”)
- reader
- r
- row
[6 Write steps to print data from csv file in list object and support your answer with example.
- Import csv module – import csv
- Open the csv file in reading mode – f = open(“demo.csv”,”r”)
- Use list object to store the data read from csv using reader – data = csv.reader(f)
- close the csv file – f.close()
- print the data object – print(data)
[6] How to print following data for cust.csv in tabular form usig python code?
SNo | Customer Name | City | Amount |
1 | Dhaval | Anand | 1500 |
2 | Anuj | Ahmedabad | 2400 |
3 | Mohan | Vadodara | 1000 |
4 | Sohan | Surat | 700 |
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()
[7] 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
[8] 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)
for field in row:
if field == custname:
record.remove(row)
with open('cust.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(record)
Share this article with your friends and classmates.
Ask your doubt in comment section.
Thank you for reading this article CSV in Python class 12 questions and answers.
Attend this quiz to check your knowledge.
I am getting windows error 32 if I run this code .