0% found this document useful (0 votes)
218 views3 pages

CSV BoardQuestions

htfht

Uploaded by

anilkoranga.pc
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)
218 views3 pages

CSV BoardQuestions

htfht

Uploaded by

anilkoranga.pc
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/ 3

Board Questions on CSV Files

1. Consider the following CSV file (emp.csv):

Sl,name,salary

1,Peter,3500 2,Scott,4000 3,Harry,5000 4,Michael,2500


5,Sam,4200
Write Python function DISPEMP( ) to read the content of file emp.csv and
display onlythose records where salary is 4000 or above
Answer:
import csv
def DISPEMP():
with open('emp.csv',”r”, newline=’’) as csvfile:
myreader = csv.reader(csvfile,delimiter=',')
print(“EMPNO \t EMP NAME \t SALARY")
for row in myreader:
if int(row[2])>4000:
print(row[0],"\t",row[1],"\t",row[2])

2. Define a function addCsvFile(UserName,PassWord) to store username and passwords


in a csv file named “user.csv”, and other function readCsvFile() to read and print the
contents of the file.

#Function calls for storing data are given below


addCsvFile(“Arjun”,”123@456”)
addCsvFile(“Arunima”,”aru@nima”)
addCsvFile(“Frieda”,”myname@FRD”)
readCsvFile()
Answer:

import csv
def addCsvFile(UserName,PassWord):#to write data into the CSV file
f=open(' user.csv','a', newline=’’)
newFileWriter = csv.writer(f, delimiter=’,’))
newFileWriter.writerow([UserName,PassWord])
f.close()
#csv file reading code
def readCsvFile():
with open(' user.csv','r', newline=’’) as newFile:
newFileReader = csv.reader(newFile, delimiter=’,’)
for row in newFileReader:
print (row[0],row[1])
#_main_
addCsvFile(“Arjun”,”123@456”)
addCsvFile(“Arunima”,”aru@nima”)
addCsvFile(“Frieda”,”myname@FRD”)
readCsvFile()
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 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.
As a Python expert, help him complete the task
Answer:
import csv
import csv
def wonCount( ):
f= open (‘Result.csv’ , ‘r’, newline=’’)
R= csv.reader(f,delimiter=’,’)
C=0
for rec in R:
if rec[3]==’won’:
C=C+1
print(“Number of events won= “,C)
f.close()

You might also like