[go: up one dir, main page]

0% found this document useful (0 votes)
75 views9 pages

CSV Files Worksheet Ans

The document contains various Python programming exercises focused on handling CSV files, including functions for adding and searching records. It features examples of code for managing employee, courier, book, and peripheral device records, along with questions and answers related to CSV file operations. Additionally, it emphasizes the importance of CSV files being human-readable and provides comparisons with binary files.

Uploaded by

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

CSV Files Worksheet Ans

The document contains various Python programming exercises focused on handling CSV files, including functions for adding and searching records. It features examples of code for managing employee, courier, book, and peripheral device records, along with questions and answers related to CSV file operations. Additionally, it emphasizes the importance of CSV files being human-readable and provides comparisons with binary files.

Uploaded by

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

CSV FILES - WORKSHEET

SP 2021 -22
1

Ans) c
2

Ans) d
3

Ans)b
4

Ans)a
5

Ms Maya Giby
PGT Computer Sc
i

Ans)c
ii

Ans)b
iii

Ans)c
iv

Ans)d
v

Ans)c
vi

Ans)c
SP 2022 -23

Ms Maya Giby
PGT Computer Sc
6

Ans)a
7

Ans)It is human readable – can be opened in Excel and Notepad applications.


It is just like text file
import csv
def ADD():
F = open(‘record.csv’,’a’,newline=’’)
W = csv.writer(F)
Empid = int(input(‘Enter employee id =’))
Name = input(‘Enter name =’)
Mobile = int(input(‘Enter mobile no =’))
L = [Empid, Name, Mobile]
W.writerow(L)
F.close()
def COUNTR():
F= open(‘record.csv’, ‘r’)
R = csv.reader(F)
L = list(R )
print(‘Number of records =’, len(L))
F.close()
ADD()
COUNTR()

Binary file CSV file


1)Extension is .dat 1)Extension is .csv
2)Not human readable 2)Human readable
3)Stores data in the form of 0s and 1s 3)Stores data like a text file

import csv
def add():
Ms Maya Giby
PGT Computer Sc
F = open(‘furdata.csv’ , ‘a’, newline =’’)
W = csv.writer(F)
W.writerow([‘fid’,’fname’,’fprice’])
fid = int(input(‘Enter furniture id =’))
fname = input(‘Enter furniture name =’)
fprice = int(input(‘Enter price =’))
data =[fid,fname,fprice]
W.writerow(data)
F.close()
def search():
F = open(‘furdata.csv’, ‘r’)
R = csv.reader(F)
Found = 0
for i in R:
if int(i[2]) > 10000:
print(i)
Found = 1
if Found == 0:
print(‘Record not found’)
F.close()
add()
search()
TERM1 2022 BOARD
9 Which of the following function is used with the csv module in Python to read the contents
of a csv file into an object?
a)readrow() b)readrows() c)reader() d)load()
Ans)reader()
10 Nisha , an intern in ABC Pvt Ltd, is developing a project using the csv module in
Python. She has partially developed the code as follows leaving out statements about
which she is not very confident. The code also contains errors in certain statements.
Help her in completing the code to read the desired CSV File named ‘Employee.csv”

#incomplete code with errors


import CSV #statement1
with open (_____, _______, newline=’’) as File: #Statement 2
ER = csv. ________ #Satatement 3
for R in range(ER): #Statement 4
if _____ == ‘ACCOUNTS’: #Statement 5
print( ______ , ______) #Statement 6

i Nisha get an error for the module name used in Statement-1. What should she write in
place of CSV to import the correct module?
a)file b)csv c)Csv d)pickle
ii Identify the missing code for blank spaces in the line marked as Statement-2 to open the
mentioned file.
a)”Employee.csv”, “r” b)”Employee.csv”,”w”
c)”Employee.csv”,”rb” d)”Employee.csv”,”wb”

Ms Maya Giby
PGT Computer Sc
iii Choose the function name (with parameter) that should be used in the line marked as
Statement – 3
a)reader(File) b)readrows(File) c)writer(File) d)writerows(File)

iv

vi Identify the suitable code for blank space in Statement-6 to display every Employee’s Name
and corresponding Department?
a)ER[1],R[2] b)R[1],ER[2] c)R[1],R[2] d)ER[1], ER[2]
BOARD 2022-23
11 a)Write one difference between CSV and text files.
Ans)CSV file represents data in a tabular form separated by comma and text files
represent data in plain text form.

Write a program in python that defines and calls the following user -defined functions:
1.COURIER_ADD() :It takes the values from the user and adds the details to a csv
file ‘courier.csv’ . Each record consists of a list with field elements as a cid, s_name,
source and destination to store courierID, Sender name, Source and destination address
respectively.
2.COURIER_SEARCH() : Takes the destination as the input and displays all the
courier records going to that destination.

import csv
def COURIER_ADD():
F = open(‘courier.csv’ , ‘a’, newline =’’)
W = csv.writer(F)
W.writerow([‘cid’,’s_name’,’source’,’destination’])
cid = int(input(‘Enter courier id =’))
name = input(‘Enter sender name =’)
source = input(‘Enter source =’)
destination = input(‘Enter destination = ‘)
data =[cid,name,source,destination]
W.writerow(data)
F.close()

def COURIER_SEARCH():
F = open(courier.csv’ , ‘r’)
R = csv.reader(F)
D = input(‘Enter destination =’)
for i in R:
if i[3] == D:
print(i)
F.close()

Ms Maya Giby
PGT Computer Sc
COURIER_ADD()
COURIER_SEARCH()

OR
b)Why it is important to close a file before exiting?
Ans)

Write a program in Python that defines and calls the following user- defined functions:
i)Add_Book() : Takes the details of the books and adds them to a csv file ‘Book.csv’.
Each record consists of a list with field elements as book_ID, B_name and pub to store
bookID, bookName and publisher respectively.
ii)Search_Book() :Takes publisher name as input and counts and displays the number
of books published by them.

import csv
def Add_Book():
F = open(‘Book.csv’,’a’,newline=’’)
W = csv.writer(F)
Book_ID = int(input(‘Enter book id =’))
B_name = input(‘Enter book name =’)
pub = input(‘Enter publisher name =’))
L = [Book_ID, B_name, pub]
W.writerow(L)
F.close()

def Search_Book():
F= open(‘Book.csv’, ‘r’)
R = csv.reader(F)
Pno = input(‘Enter publisher =’)
Count = 0
for i in R:
if i[2] == Pno:
Count += 1
print(‘Total number of books published by ’, Pno ,’=’ ,Count)
F.close()
Add_Book()
Search_Book()

SP 2023-24
12

Ms Maya Giby
PGT Computer Sc
import csv
def Accept():
F = open(‘Result.csv’, ‘a’, newline =’’)
W=csv.writer(F)
W.writerow([‘St_Id’,’ST_name’,’Game_Name’,’Result’])
St_Id = int(input(‘Enter student Id =’))
ST_name = input(‘Enter student name =’)
Game_Name = input(‘Enter game name =’)
Result = input(‘Enter result =’)
Data = [St_Id, ST_name, Game_Name, Result]
W.writerow(Data)
F.close()

def wonCount():
F = open(‘Result.csv’, ‘r’)
R = csv.reader(F)
Count = 0
for i in R:
if i[3] == ‘won’:
Count += 1
print(‘No. of students who have won any event =’, Count)
F.close()

Accept()
wonCount()

BP 2023-24
13 Assertion :CSV file is a human readable text file where each line has a number of fields, separated
by comma or some other delimiter.
Reason : writerow() method is used to write a single row in a CSV file.
Ans)(b) Both (A) and (R) are true and (R ) is not the correct explanation for (A )
14 Sangeetha is a Python programmer working in a computer hardware company. She has to maintain
the records of the peripheral devices. She created a csv file named Peripheral.csv, to store the
details.The structure of Peripheral.csv is :[P_id, P_name, Price] where
P_id is Peripheral device ID (integer)
P_name is Peripheral device name (String)
Price is Peripheral device price (integer)
Ms Maya Giby
PGT Computer Sc
Sangeetha wants to write the following user defined functions:
Add_Device() : to accept record from the user and add it to a csv file, Peripheral.csv
Count_Device() :to count and display number of peripheral devices whose price is less than 1000.

import csv
def Add_Device():
F = open(‘Peripheral.csv’, ‘a’, newline=’’)
W = csv.writer(F)
P_id = int(input(‘Enter the peripheral id =’))
P_name = input(‘Enter peripheral name =’)
Price = int(input(‘Enter price =’))
Data =[P_id, P_name, Price]
W.writerow(Data)
F.close()
def Count_Device():
F = open(‘Peripheral.csv’, ‘r’)
R =csv.reader(F)
Count = 0
for i in R:
if int(i[2]) <1000:
Count += 1
print(‘No of peripheral devices whose price is less than 1000 =’ , Count)
F.close()
Add_Device()
Count_Device()
SP 2024-25
15

import csv
def show():
F = open(‘happiness.csv’, ‘r’)
R = csv.reader(F)
next(R, None) # To skip the header row
for i in R:
if int(i[1]) >5000000:
print(i)
F.close()
def COUNT():
F = open(‘happiness.csv’, ‘r’)
Ms Maya Giby
PGT Computer Sc
R = csv.reader(F)
count = 0
for i in R:
count += 1
print(‘No of records =’, count)
F.close()
show()
COUNT()

Ms Maya Giby
PGT Computer Sc

You might also like