CSV Files Worksheet
CSV Files Worksheet
1. Which of the following mode in file opening statement results or generates an error if the file
does not exist?
a) a+ (b)r+ (c)w+ (d) None of theabove
4. Which pickle module method is used to write a Python object to a binary file?
a. save() b. serialize() c. store() d. dump()
5. Which of the following file opening mode in Python, generates an error if the file does
not exist?
a) a b) r c) w d) w+
6. Which of the following functions changes the position of file pointer and returns its
new position?
a) flush() b) tell() c) seek() d) offset()
8. Which of the following options can be used to read the first line of a text file Myfile.txt?
a. myfile = open('Myfile.txt'); myfile.read()
b. myfile = open('Myfile.txt','r'); myfile.read(n)
c. myfile = open('Myfile.txt'); myfile.readline()
d. myfile = open('Myfile.txt'); myfile.readlines()
9. Assume that the position of the file pointer is at the beginning of 3rd line in a text file.
Which of the following option can be used to read all the remaining lines?
a. myfile.read() b. myfile.read(n)
c. myfile.readline() d. myfile.readlines()
10. A text file student.txt is stored in the storage device. Identify the correct option out of the
following options to open the file in read mode.
i. myfile = open('student.txt','rb')
ii. ii. myfile = open('student.txt','w')
iii. myfile = open('student.txt','r')
iv. iv. myfile = open('student.txt')
14. Syntax of seek function in Python is myfile.seek( offset, reference_point) where myfile is
the file object. What is the default value of reference_point?
a. 0 b. 1 c. 2 d. 3
15. Which of the following character acts as default delimiter in a csv file?
a. (colon) :
b. (hyphen) –
c. (comma) ,
d. (vertical line) |
18. Which of the following is not a function / method of csv module in Python?
a. read()
b. reader()
c. writer()
d. writerow()
19. Which one of the following is the default extension of a Python file?
a. .exe
b. .p++
c. .py
d. .p
21.
22. Which of the following methods will give the current position of the file pointer?
(a)seek()
(b)tell()
c)getloc()
(d) None of the above
b) Write a Program in Python that defines and calls the following user defined functions:
ADDPROD() – To accept and add data of a product to a CSV file ‘product.csv’. Each record
consists of a list with field elements as prodid, name and price to store product id, product name
and product price respectively.
COUNTPROD() – To count the number of records present in the CSV file named ‘product.csv’.
2.
a) Give any one point of difference between a binary file and a csv file.
b) Write a Program in Python that defines and calls the following user defined functions:
. (i) add() – To accept and add data of a to a CSV file ‘stud.csv’. Each record consists of a list
with field elements as admno, sname and per to store admission number, student name and
percentage marks respectively.
(ii) search()- To display the records of the students whose percentage is more than 75.
3)Vedansh is a Python programmer working in a school. For the Annual Sports Event, he has
created a csv file named Result.csv, to store the results of students in different sports events.
For efficiently maintaining data of the event, Vedansh wants to write the following user defined
functions:
Accept() – to accept a record from the user and add it to the file Result.csv. The column
headings should also be added on top of the csv file.
wonCount() – to count the number of students who have won any event.
4.
a) Give any one point of difference between a binary file and a csv file.
b) Write a Program in Python that defines and calls the following user defined functions:
add() – To accept and add data of an employee to a CSV file ‘furdata.csv’. Each record consists
of a list with field elements as [fid, fname, fprice] to store furniture id, furniture name and
furniture price respectively.
search()- To display the records of the furniture whose price is more than 10000.
The structure of the file is: [ Emp_id, Emp_Name, Emp_Designtion,No of Incentives received ]
He needs to write a user defined functions that performs the following tasks
Accept() – to accept the details from the user and add it to the file.
Incentive() – to display the name of employee who received incentive more than 3 times.
*The file “incentive.csv” should have a proper title.