Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Other

CSV File type in Python Question

Rating
-
Sold
-
Pages
9
Uploaded on
25-01-2022
Written in
2020/2021

It is a question bank for CSV file in Python which also includes the answer key.

Institution
Course

Content preview

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

Written for

Institution
Course

Document information

Uploaded on
January 25, 2022
Number of pages
9
Written in
2020/2021
Type
OTHER
Person
Unknown

Subjects

$7.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
samarthvohra2003

Get to know the seller

Seller avatar
samarthvohra2003 JIMS Engineering Management Technical Campus
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
4 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions