Data File Handling - Worksheet 1 - 5 Marks
Data File Handling - Worksheet 1 - 5 Marks
1.
2.
2. Write the full form of ‘CSV’. What is the default delimiter of csv files? The scores and ranks of
three students of a school level programming competition is given as: [‘Name’, ‘Marks’, ‘Rank’]
[‘Sheela’, 450, 1]
[‘Rohan’, 300, 2]
[‘Akash’, 260, 3]
Write a program to do the following:
(i) Create a csv file (results.csv) and write the above data into it.
(ii) To display all the records present in the CSV file named ‘results.csv’
OR
What does csv.writer object do?
3. Rohan 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).
Help him to define and call the following user defined functions:
(i) AddNewRec(Country,Capital) – To accept and add the records to a CSV file
“CAPITAL.CSV”. Each record consists of a list with field elements as Country and
Capital to store country name and capital name respectively.
(ii) ShowRec() – To display all the records present in the CSV file named ‘CAPITAL.CSV’
Write the following functions in python accommodate the data and manipulate it.
a) A function insert() that creates the data.dat file in your system and writes the three
dictionaries.
b) A function() read() that reads the data from the binary file and displays the dictionaries whose
age is 16.
5.(a) What does CSV stand for?
(b) Write a Program in Python that defines and calls the following user defined functions:
(i) InsertRow() – To accept and insert data of an student to a CSV file ‘class.csv’. Each record
consists of a list with field elements as rollno, name and marks to store roll number, student’s
name and marks respectively.
(ii) COUNTD() – To count and return the number of students who scored marks greater than 75
in the CSV file named ‘class.csv’
6. Dhirendra is a programmer, who has recently been given a task to write a python code to
perform the following CSV file operations with the help of two user defined functions/modules:
a. CSVOpen() : to create a CSV file called BOOKS.CSV in append mode containing information of
books – Title, Author and Price.
b. CSVRead() : to display the records from the CSV file called BOOKS.CSV where the field title
starts with 'R'. He has succeeded in writing partial code and has missed out certain statements,
so he has left certain queries in comment lines.
importcsv
def CSVOpen():
with open('books.csv','_____',newline='') as csvf: #Statement-1
cw=_____________#Statement-2
__________________#Statement-3
cw.writerow(['Rapunzel','Jack',300])
cw.writerow(['Barbie','Doll',900])
cw.writerow(['Johnny','Jane',280])
def CSVRead():
try:
with open('books.csv','r') as csvf:
cr=_________________#Statement-4
for r in cr: if : #Statement-5
addCsvFile(“Aman”,”123@456”)
addCsvFile(“Vijay”,”aru@nima”)
addCsvFile(“Raju”,”myname@FRD”)
readCsvFile()
(i) Give Name of the module he should import in Line 1.
(ii) In which mode, Vijay should open the file to add data into the file in Line2.
(iii) Complete the Line 3 to read the data from csv file and Line 4 to close the file
14. What is the significance of a delimiter symbol in a csv file? Write a Program in Python that
defines and calls the following user defined functions:
(i) ADD_CONT() – To accept and add data of a contact to a CSV file ‘address.csv’. Each record
consists of a list with field elements as contId, cname and cmobile to store contact id, contact
name and contact number respectively.
(ii) COUNT_CONT() – To count the number of records present in the CSV file named ‘address.csv’
15. How csv file is different from a binary file? Write a Program in Python that defines and calls
the following user defined functions:
(i) save() – To accept and add data of watches to a CSV file ‘watchdata.csv’. Each record consists
of a list with field elements as watchid, wname and wprice to store watch id, watch name and
watch price respectively.
(ii) search()- To display the records of the watch whose price is more than 6000.
16. Vaishanavi is a budding Python programmer. She has written a code and created a binary
file phonebook.dat with contactNo, name and blocked [ Y/ N ]. The file contains 10 records as a
dictionary like {‘contactNo’ : 32344455 , ‘name’: ‘kamalkant’ ,’blocked’ : “Y” }
She now wants to shift all the records which have blocked = ‘Y’ status from phonebook.dat to a
binary file blocked.dat also all records which have blocked = ‘N’ status from phonebook.dat to
unblocked.dat. She also wants to keep count and print the total number of blocked and
unblocked records. As a Python expert, help her to complete the following code based on the
requirement given above:
import _____________ #Statement 1
def shift_contact( ):
fin = open(“phonebook.dat”,’rb’)
fblock = open( ___________________ ) #Statement 2
funblock = open( ___________________ ) #Statement 3
while True :
try: rec = __________________ # Statement 4