Previous Index Next

Review Questions - CSV File Handling

1. What is the correct expansion of CSV files?
a. Comma Separable Values
b. Comma Separated Values
c. Comma Split Values
d. Comma Separation Values


2. Which of the following character acts as default delimiter in a csv file?
a. (colon) :
b. (hyphen) -
c. (comma) ,
d. (vertical line) |


Which of the following is not a function / method of csv module in Python?
a. read()
b. reader()
c. writer()
d. writerow()


4. 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


5. Write the name of module provided by Python to do several operations on the CSV files.


6. Which of the following creates an object which maps data to a dictionary?
a. listreader()
b. reader()
c. tuplereader()
d. DictReader()


7. What is the difference between reader() and DictReader() function?


8. Write the statement to open STUDENT.CSV in append mode in such a way there should be no blank line between each row.


9. Vijay 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

# to write / add data into the CSV file
def addRecord(user,password):
    file=open('users.csv','___',newline='') # Line 2
    cwriter = csv.writer(file)
    cwriter.writerow([user,password])
    file.close()

#csv file reading code
def readRecord(): # to read data from CSV file
    f=open('users.csv','r')
    creader = csv._________(f) # Line 3
    for row in creader:
        print (row[0],row[1])
    __________ # Line 4

addRecord("Neil", "123@456")
addRecord("Swati","sw@tee")
addRecord("Farida","myname@FRD")
readRecord() #Line 5

(a) Name the module he should import in Line 1.


(b) In which mode, Vijay should open the file to append 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.


10. 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.

CSV File
ROLL_NO,NAME,CLASS,SECTION
1,AKSHAY,XII,A
2,ABHISHEK,XII,A
3,ARVIND,XII,A
4,RAVI,XII,A
5,ASHISH,XII,A

Incomplete Code

import _____ #Statement-1
fh = open(_____, _____, newline='') #Statement-2
stuwriter = 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 : ")
    section = input("Enter Section : ")
    rec = [ _____ ] #Statement-4

data.append(_____) #Statement-5
stuwriter. _____ (data) #Statement-6
fh.close()

i) Identify the suitable code for blank space in the line marked as Statement-1.
a) csv file
b) CSV
c) csv
d) cvs


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"


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)


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


v) Identify the suitable code for blank space in the line marked as Statement-5.
a) data
b) record
c) rec
d) insert


vi) Choose the function name that should be used in the blank space of line marked as a) dump()
b) load()
c) writerows()
d) writerow()



Previous Index Next