0% found this document useful (0 votes)
62 views4 pages

CSV Files Worksheet

Uploaded by

deepa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views4 pages

CSV Files Worksheet

Uploaded by

deepa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

BINARY AND CSV FILES

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

2.The correct syntax of seek() is:


a)file_object.seek(offset [, reference_point] )
b) seek(offset [, reference_point])
c) seek(offset, file_object)
d ) seek.file_object(offset)

3.Which of the following is an invalid access mode for text files:


a) w b) a+ c) ab d) r

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()

7. Which of the following option is not correct?


a. if we try to read a text file that does not exist, an error occurs.
b. if we try to read a text file that does not exist, the file gets created.
c. if we try to write on a text file that does not exist, no error occurs.
d. if we try to write on a text file that does not exist, the file gets Created.

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')

a. only I b. both i and iv c. both iii and iv d. both i and iii


11. Which of the following statement is incorrect in the context of binary files?
a. Information is stored in the same format in which the information is held in memory.
b. No character translation takes place
c. Every line ends with a new line character
d. pickle module is used for reading and writing

12. What is the significance of the tell() method?


a. tells the path of file
b. tells the current position of the file pointer within the file
c. tells the end position within the file
d. checks the existence of a file at the desired location

13. Which of the following statement is true?


a. pickling creates an object from a sequence of bytes
b. pickling is used for object serialization
c. pickling is used for object deserialization
d. pickling is used to manage all types of files in Python

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) |

16. Syntax for opening Student.csv file in write mode is


myfile = open("Student.csv","w",newline='').
What is the importance of newline=''?
a. A newline gets added to the file b. Empty string gets appended to the first line.
c. Empty string gets appended to all lines. d. EOL translation is suppressed

17. What is the correct expansion of CSV files?


a. Comma Separable Values
b. Comma Separated Values
c. Comma Split Values
d. Comma Separation Values

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

23. The default mode of opening a file in pyhton


(a) append (b) read (c) write (d) both b and c

CSV FILES PROGRAMS

1. a) What is the advantage of using a csv file for permanent storage?

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.

The structure of Result.csv is : [St_Id, St_Name, Game_Name, Result]

Where St_Id is Student ID (integer)


ST_name is Student Name (string)
Game_Name is name of game in which student is participating(string)
Result is result of the game whose value can be either 'Won', 'Lost' or 'Tie'

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.

5. Raju is a python programmer working in a leading software company in Chennai. He is


assigned with a task to provide the employee name for their annual appreciation with incentive.
For this he created a csv file named “incentive.csv” to store the names of the employee in
different criteria.

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.

You might also like