DoyPyEdu PLAY WITH PYTHON
CSV File
CSV (Comma separated values) , .csv open in excel
Sample Questions read carefully all lines
Q1. Mitaali of class 12 is writing a program to create a CSV file “user.csv” which will contain user name and
password for some entries. She has written the following code. As a programmer, help her to successfully execute
the given task.
import csv # csv module
def addCsvFile (UserName , PassWord ): # to write / add data into the CSV file ‘w’,’a’
file = open (‘user.csv’, ‘a’) #open file
newFileWriter = csv . writer ( file ) #create object
newFileWriter . writerow ( [ UserName, PassWord]) #write row in csv file
f . close ( ) #close file
# csv file reading code
def readCsvFile ( ): #function for read
with open (‘user.csv’, ‘r’) as newFile: #open file and create object
newFileReader = csv .reader (newFile) #read data from file
for row in newFileReader : #loop for read rows from csv
print (row[0], row [1]) #display |Show data on run time
newFile . close() #close file
addCsvFile (“Biplab”, “123@456”) #calling of AddCsvFile for write data in csv
addCsvFile (“Arunima”, “aru@nima”) #calling of AddCsvFile for write data in csv
addCsvFile (“Poonam”, myname@FRD”) #calling of AddCsvFile for write data in csv
readCsvFile ( ) #calling of readCsvFile for display
Solved Questions
Q1. Abhisar is making a software on “Countries & their Capitals” in which various
records are to be stored/retrieved in CAPITAL.CSV data file. It consists some
records(Country & Capital). He has written the following code in python. As a
programmer, you have to help him to successfully execute the program.
import ___________ # Statement-1
def AddNewRec(Country,Capital): # Fn. to add a new record in CSV file
f=open(“CAPITAL.CSV”,_________) # Statement-2
fwriter=csv.writer(f)
fwriter.writerow([Country,Capital])
f.____________ # Statement-3
def ShowRec(): # Fn. to display all records from CSV file
with open(“CAPITAL.CSV”,”r”) as NF:
NewReader=csv.___________(NF) # Statement-4
for rec in NewReader:
print(rec[0],rec[1])
AddNewRec(“INDIA”,”NEW DELHI”)
AddNewRec(“CHINA”,”BEIJING”)
ShowRec() # Statement-5
(a) Name the module to be imported in Statement-1. csv
(b) Write the file mode to be passed to add new record in Statement-2. ‘a’
(c) Fill in the blank in Statement-3 to close the file. Close()
(d) Fill in the blank in Statement-4 to read the data from a csv file. Readrer()
Page 1 of 9 EDUCATION FOR EVERYONE
, DoyPyEdu PLAY WITH PYTHON
(e) Write the output which will come after executing Statement-5. INDIA NEW DELHI
CHINA BEIJING
Q2. Sanjay Dalmia of class 12 is writing a program to create a CSV file “contacts.csv” which will contain Name and
Mobile Number 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(Name,Mobile): # to write / add data into the CSV file
f=open('contacts.csv',' ') # Line 2
newFileWriter = csv.writer(f)
newFileWriter.writerow([Name,Mobile])
f.close()
#csv file reading code
def readCsvFile(): # to read data from CSV file with open('contacts.csv','r') as newFile:
newFileReader = csv. (newFile) # Line 3 for row in newFileReader:
print (row[0],row[1])
newFile. # Line 4
addCsvFile(“Arjun”,”8548587526”)
addCsvFile(“Arunima”,”6585425855”)
addCsvFile(“Frieda”,”8752556320”)
readCsvFile() #Line 5
a) Name the module he should import in Line 1. import csv
b) In which mode, Sanjay should open the file to add data into the file a or a+
c) Fill in the blank in Line 3 to read the data from a csv file. reader
d) Fill in the blank in Line 4 to close the file. close()
a) Write the output he will obtain while executing Line 5. Arjun 8548587526
Arunima 6585425855
Frieda 8752556320
Q3. Anis of class 12 is writing a program to create a CSV file “mydata.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(' mydata.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('mydata.csv','r') as newFile:
newFileReader = csv._________(newFile) # Line 3
for row in newFileReader:
print (row[0],row[1])
newFile.______________ # Line 4
addCsvFile(“Aman”,”123@456”)
addCsvFile(“Anis”,”aru@nima”)
addCsvFile(“Raju”,”myname@FRD”)
readCsvFile() #Line 5
(a) Give Name of the module he should import in Line 1. csv
(b) In which mode, Aman should open the file to add data into the file a
Page 2 of 9 EDUCATION FOR EVERYONE
CSV File
CSV (Comma separated values) , .csv open in excel
Sample Questions read carefully all lines
Q1. Mitaali of class 12 is writing a program to create a CSV file “user.csv” which will contain user name and
password for some entries. She has written the following code. As a programmer, help her to successfully execute
the given task.
import csv # csv module
def addCsvFile (UserName , PassWord ): # to write / add data into the CSV file ‘w’,’a’
file = open (‘user.csv’, ‘a’) #open file
newFileWriter = csv . writer ( file ) #create object
newFileWriter . writerow ( [ UserName, PassWord]) #write row in csv file
f . close ( ) #close file
# csv file reading code
def readCsvFile ( ): #function for read
with open (‘user.csv’, ‘r’) as newFile: #open file and create object
newFileReader = csv .reader (newFile) #read data from file
for row in newFileReader : #loop for read rows from csv
print (row[0], row [1]) #display |Show data on run time
newFile . close() #close file
addCsvFile (“Biplab”, “123@456”) #calling of AddCsvFile for write data in csv
addCsvFile (“Arunima”, “aru@nima”) #calling of AddCsvFile for write data in csv
addCsvFile (“Poonam”, myname@FRD”) #calling of AddCsvFile for write data in csv
readCsvFile ( ) #calling of readCsvFile for display
Solved Questions
Q1. Abhisar is making a software on “Countries & their Capitals” in which various
records are to be stored/retrieved in CAPITAL.CSV data file. It consists some
records(Country & Capital). He has written the following code in python. As a
programmer, you have to help him to successfully execute the program.
import ___________ # Statement-1
def AddNewRec(Country,Capital): # Fn. to add a new record in CSV file
f=open(“CAPITAL.CSV”,_________) # Statement-2
fwriter=csv.writer(f)
fwriter.writerow([Country,Capital])
f.____________ # Statement-3
def ShowRec(): # Fn. to display all records from CSV file
with open(“CAPITAL.CSV”,”r”) as NF:
NewReader=csv.___________(NF) # Statement-4
for rec in NewReader:
print(rec[0],rec[1])
AddNewRec(“INDIA”,”NEW DELHI”)
AddNewRec(“CHINA”,”BEIJING”)
ShowRec() # Statement-5
(a) Name the module to be imported in Statement-1. csv
(b) Write the file mode to be passed to add new record in Statement-2. ‘a’
(c) Fill in the blank in Statement-3 to close the file. Close()
(d) Fill in the blank in Statement-4 to read the data from a csv file. Readrer()
Page 1 of 9 EDUCATION FOR EVERYONE
, DoyPyEdu PLAY WITH PYTHON
(e) Write the output which will come after executing Statement-5. INDIA NEW DELHI
CHINA BEIJING
Q2. Sanjay Dalmia of class 12 is writing a program to create a CSV file “contacts.csv” which will contain Name and
Mobile Number 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(Name,Mobile): # to write / add data into the CSV file
f=open('contacts.csv',' ') # Line 2
newFileWriter = csv.writer(f)
newFileWriter.writerow([Name,Mobile])
f.close()
#csv file reading code
def readCsvFile(): # to read data from CSV file with open('contacts.csv','r') as newFile:
newFileReader = csv. (newFile) # Line 3 for row in newFileReader:
print (row[0],row[1])
newFile. # Line 4
addCsvFile(“Arjun”,”8548587526”)
addCsvFile(“Arunima”,”6585425855”)
addCsvFile(“Frieda”,”8752556320”)
readCsvFile() #Line 5
a) Name the module he should import in Line 1. import csv
b) In which mode, Sanjay should open the file to add data into the file a or a+
c) Fill in the blank in Line 3 to read the data from a csv file. reader
d) Fill in the blank in Line 4 to close the file. close()
a) Write the output he will obtain while executing Line 5. Arjun 8548587526
Arunima 6585425855
Frieda 8752556320
Q3. Anis of class 12 is writing a program to create a CSV file “mydata.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(' mydata.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('mydata.csv','r') as newFile:
newFileReader = csv._________(newFile) # Line 3
for row in newFileReader:
print (row[0],row[1])
newFile.______________ # Line 4
addCsvFile(“Aman”,”123@456”)
addCsvFile(“Anis”,”aru@nima”)
addCsvFile(“Raju”,”myname@FRD”)
readCsvFile() #Line 5
(a) Give Name of the module he should import in Line 1. csv
(b) In which mode, Aman should open the file to add data into the file a
Page 2 of 9 EDUCATION FOR EVERYONE