Worksheet 2 File Handling
Worksheet 2 File Handling
1. What does the acronym CSV stand for in its full form?
a. Common Separated Value
b. Comma System Value
c. Comma Separated Value
d. Common System Vault
Ans: c. Comma Separated Value
Ans: b. Comma
3. In regards to separated value files such as .csv and .tsv, what is the delimiter?
a. Any character such as the comma (,) or tab (\t) that is used to separate
the row data
b. Anywhere the comma (,) character is used in the file
c. Delimiters are not used in separated value files
d. Any character such as the comma (,) or tab (\t) that is used to separate
the column data.
Ans: d. Any character such as the comma (,) or tab (\t) that is used to separate
the column data.
4. In separated value files such as .csv and .tsv, what does the first row in the file
typically contain?
a. The source of the data
b. The author of the table data
c. Notes about the table data
d. The column names of the data
5. Assume you have a file object my_data which has properly opened a separated
value file that uses the tab character (\t) as the delimiter.
What is the proper way to open the file using the Python csv module and assign it
to the variable csv_reader?
Assume that csv has already been imported.
a. csv_reader = csv.tab_reader(my_data)
b. csv_reader = csv.reader(my_data)
c. csv_reader = csv.reader(my_data, tab_delimited=True)
d. csv_reader = csv.reader(my_data, delimiter='\t')
6. When iterating over an object returned from csv.reader(), what is returned with
each iteration?
For example, given the following code block that assumes csv_reader is an
object
returned from csv.reader(), what would be printed to the console with each
iteration?
for item in csv_reader:
print(item)
1. Write a program in python to write and read structure, dictionary to the binary
file.
import pickle
d1={'jan':31,'feb':28,'march':31,'april':30}
f=open('binfile.dat','wb+')
pickle.dump(d1,f)
d2=pickle.load(f)
print(d2)
f.close()
2. BINARY file is unreadable and open and close through a function only so what are
the advantages of using binary file
Binary file are easier and faster than text file. Binary files are also used
to store binary data such as images, video files, and audio files.
3. Write a statement to open a binary file name sample.dat in read mode and the file
sample.dat is placed in a folder ( name school) existing in c drive
f1=open(“c:\school\sample.dat”,’r’)
4. When do you think text files should be preferred over binary files?
Text file should be preferred when we have to save data in text format andsecurity of
file is not important
Neha is making software on “Items & their prices” in which various records are to be
stored/retrieved in STORE.CSV data file. It consists some records (Item & Price).
She has written the following code in python. As a programmer, you have to help her
to successfully execute the program.
import # Statement-1
def AddItem(Item, Price) # Statement-2
f=open(“STORE.CSV”, ) # Statement-3
fw=csv.writer(f)
fw.writerow([Item, Price])
# Statement-4
def ShowRecord():
with open(“STORE.CSV”,”r”) as NI:
NewItem=csv. (NI) # Statement-5
for rec in NewItem:
print(rec[0], “#”, rec[1])
#main-code
AddItem(“Sugar”, 38.00)
AddItem(“Rice”, 48.50)
ShowRecord() # Statement-6
Ans: B. csv
Ans: D. a
Ans: C. f.close()
Q4. Which function to be used in Statement-5 to read the data from a csv
file.
A. read() B. readline() C. readlines() D.
reader()Ans: D. reader()
23 Ranjan 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 _____________
csv # Line 1
(b) In which mode, Ranjan should open the file to add data into the file a
(c) Fill in the blank in Line 3 to read the data from a csv file. reader
import pickle
def CreateEmp():
f1=open("C:\\xii_ip\\emp.dat",'wb')
eid=input("Enter E. Id")
ename=input("Enter Name")
designation=input("Enter Designation")
salary=int(input("Enter Salary"))
l=[eid,ename,designation,salary]
pickle.dump(l,f1)
f1.close()
import pickle
def display():
f2=open("C:\\xii_ip\\emp.dat","rb")
try:
while True:
rec=pickle.load(f2)
if rec[3]>5000:
print(rec[0],rec[1],rec[2],rec[3])
except:
f2.close()
display()
i. A binary file “emp.DAT” has structure (EID, Ename, designation,salary). Write a function to
add more records of employes in existing file emp.dat.
import pickle
def createemp:
f1=open("emp.dat",'ab')
eid=input("Enter E. Id")
ename=input("Enter Name")
designation=input("Enter Designation")
salary=int(input("Enter Salary"))
l=[eid,ename,designation,salary]
pickle.dump(l,f1)
f1.close()
ii. Write a function Show() in Python that would read detail of employee from file “emp.dat”
and display the details of those employee whose designation is “Salesman”.
def display():
f2=open("emp.dat","rb")
try:
while True:
rec=pickle.load(f2)
if (rec[2]=='Manager'):
print(rec[0],rec[1],
rec[2],rec[3])
except:
break
f2.close()