0% found this document useful (0 votes)
209 views10 pages

33 CSV File SQP

Hhidihhsjsjsjhsbsksoaohvwbsbsbvdvvvvv3yyyyy iselfjilldkjdh

Uploaded by

HONEY YOYO
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)
209 views10 pages

33 CSV File SQP

Hhidihhsjsjsjhsbsksoaohvwbsbsbvdvvvvv3yyyyy iselfjilldkjdh

Uploaded by

HONEY YOYO
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/ 10

CSV File:- 5 MARKS EXPECTED

● CSV file: import csv module, open / close csv file, write into a csv file using csv.writer()
and read from a csv file using csv.reader( )

CSV stands for Comma Separated Values


CSV file is a type of plain text file means data stored in form of ASCII or Unicode characters
Each line is a row and in row each piece of data is separated by a comma
It is common format for data interchange
csv module provides classes that assist in the reading and writing of Comma Separated Value (CSV) files
Statement to import csv module:-
import csv
To open a csv file, use open() function as used previously in text and csv file.
Syntax:- file_object=open(‘filename.csv', ‘mode')
f=open('data.csv', 'w')
csv.reader :- It is a built-in function in module csv. It takes file object as an argument and the returned object is an
iterator. Each iteration returns a row of the CSV file
Syntax:- csv_reader = csv.reader( file_object)
csv.writer:-It is a built-in function in module csv. It takes file object and returns a writer object responsible for
converting the user’s data into delimited strings. Writer object have public methods which are used to write content
in csv file like writerow() and writerows
Syntax :- wrt=csv.writer(file_object)
wrt.writerow(list_name)
File Handling-CSV file
1. Search an employee by name from CSV file. Each record consists of a
list with field elements as empid, name , salary and dname to store
employee id, employee name, employee salary and employee
department name respectively.

import csv # csv module is used for csv file


def search(): # define function
f=open("xyz.csv",'r') # open csv file
data= csv.reader (f) # read data from csv file
n=input(“enter name to be searched”) # enter value to be searched
for i in data: # iterate through element in data
if i[1]==n: # check condition
print(i) # print or count as required
f.close() # close file

2. accept department name as parameter in search function and display


and count employees of that department from CSV file. Each record
consists of a list with field elements as empid, name , salary and dname
to store employee id, employee name, employee salary and employee
department name respectively.
# csv module is used for csv file
import csv # define function
def search(dept): # open csv file
f=open("xyz.csv",'r') # read data from csv file
data= csv.reader (f) # variable to count
count=0 # iterate through element in data
for i in data: # check condition
if i[3]== dept: # print details
print(i) # increase the count by 1
count=count+1 # display count
print (“number of employees”,count) # close file
f.close()
search(“sales”) # call search function for sales dept
3. display and count the employees whose salary is more than 50000
from CSV file. Each record consists of a list with field elements as empid,
name , salary and dname to store employee id, employee name,
employee salary and employee department name respectively.

import csv # csv module is used for csv file


def search(): # define function
f=open("xyz.csv",'r') # open csv file
data= csv.reader (f) # read data from csv file
count=0 # variable to count
for i in data: # iterate through element in data
if i[2]>50000: # check condition
print(i) # print data
count=count+1 # increase count by 1 when find a match
print (“number of employees getting more than 50000=”,count) # display count
f.close() # close file

4. Count number of record in CSV file where Each record consists of a list
with field elements as empid, name , salary and dname to store
employee id, employee name, employee salary and employee
department name respectively.

import csv # csv module is used for csv file


def search(): # define function
f=open("xyz.csv",'r') # open csv file
data= csv.reader (f) # read data from csv file
l=list(data) # convert reader object into list
print(len(l)) # print length of list
f.close() # close file

5. Write data of an employee in csv file where Each record consists of a


list with field elements as empid, name , salary and deptno to store
employee id, employee name, employee salary and employee
department number respectively.

import csv # csv module is used for csv file


def add(): # define function
f=open("xyz.csv",'a', newline='') # open csv file in append mode
wr=csv.writer(f) # create a writer object
fid=int(input("Enter Furniture Id :: "))
fname=input("Enter Furniture name :: ") # input data
fprice=int(input("Enter price :: "))
wr.writerow([fid,fname,fprice]) # write data into file
f.close() # close file
CSV FILE
Q.33 What is the advantage of using a csv file for permanent storage? Write a Program in Python that defines and
calls the following user defined functions:
(i) ADD() – To accept and add data of an employee to a CSV file ‘record.csv’. Each record consists of a list with field
elements as empid, name and mobile to store employee id, employee name and employee salary respectively.
(ii) COUNTR() – To count the number of records present in the CSV file named ‘record.csv’.
OR
Q. 33 Give any one point of difference between a binary file and a csv file. Write a Program in Python that defines
and calls the following user
defined functions:
(i) 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 and fprice to store furniture id, furniture name and furniture price respectively.
(ii) search()- To display the records of the furniture whose price is more than 10000.
Csv File
Question 1:
A csv file “Bus.csv” has structure =[ Bno , From, To]
i. Write a user defined function add() to input data for a record and add to Bus.csv .
ii. Write a function Count() in Python which count the number of bus in the csv file “Bus.csv”
iii. Write a function CountTo() in Python which count and display bus to “Delhi” are stored in the csv file
“Bus.csv”
iv. Write function to display() to display the bus from “patna” to “gaya” stored in BUS.csv file

Question 2:
A csv file “STUDENT.csv” has structure [admission_number, Name, stream,division]
i. Write a user defined function add() to input data for a record and add to “STUDENT.csv”.
ii. Write a function Count() in Python which count the number of student in the csv file “STUDENT.csv”
iii. Write a function CountRec() in Python which count and display student of Science stream are stored in the
csv file “STUDENT.csv”.
iv. Write a function Countfst() in Python which count and display student of get first division that are stored in
the csv file “STUDENT.csv”.
v. Write a function search() to search student by admission number (entered by user) from csv file
“STUDENT.csv”

Question 3:
A csv file “MEMBER.csv” has structure [M_number, M_Name, City]
i. Write a user defined function Createadd() to input data for a record and add to “MEMBER.csv”.
ii. Write a function Count() in Python which count the number of members in the csv file “MEMBER.csv”
iii. Write a function CountRec(city) in Python which accepts the city as parameter and count and display member
of that city are stored in the csv file “MEMBER.csv”.

Question 4:
A csv file GIFTS.csv has structure [ ID, Remarks, Price]
i. Write a user defined function CreateFile() to input data for a record and add to GIFTS.csv
ii. Write a function Count() in Python which count the number of gift in the csv file “GIFTS.csv”
iii. Write a function CountRec() in Python which count and display gifts of those gifts, which has remarks as “ON
DISCOUNT” are stored in the csv file GIFTS.csv

Question 5:
A csv file “LAPTOP.csv” has structure [ ModelNo, Brand , Price ]
i. Write a user defined function add() to input data for a record and add to “LAPTOP.csv”
ii. Write a function Count() in Python which count the number of Laptop in the csv file “LAPTOP.csv”
iii. Write a function CountRec(Brand) in Python which accepts the brand as parameter and count and display
laptop of that brand are stored in the csv file “LAPTOP.csv”
iv. Write a function in python to read and display the details of all the laptop which are priced between 40000
and 50000 from a csv file “LAPTOP.csv”, also display number of laptop are in this range.

Question 6:
A csv file “CUSTOMER.csv” has structure [ CNO, Cname, Gender]
i. Write a user defined function add() to input data for a record and add to “CUSTOMER.csv”.
ii. Write a function Count() in Python which count the number of customer in the csv file “CUSTOMER.csv”
iii. Write a function CountRec() in Python which count and display female customer that are stored in the csv file
“CUSTOMER.csv”

Question 7:
A csv file “CLUB.csv” has structure [ Mno, Mname, Type]
i. Write a user defined function add() to input data for a record and add to CLUB.csv
ii. Write a function Count() in Python which count the number of member stored in the csv file “CLUB.csv”
iii. Write a function search() that search a particular member by name from CLUB.csv file
iv. Write a function in python to read and display the details of all the users whose membership type is ‘L’ or ‘M’
from a csv file “CLUB.csv”, also display number of those members.

Question 8:
A csv file “FOOD.csv” has structure [ Fname, Region,Type]
i. Write a user defined function add() to input data for a record and add to FOOd.csv , enter veg for vegetarian
and nonveg for non-vegetarian
ii. Write a function Count() in Python which count the number of food stored in the csv file “FOOD.csv”
iii. Write a function CountRec() in Python which accept region as parameter and count and display food of those
region are stored in the csv file FOOD.csv
iv. Write a function in python to read and display the details of veg food from south from a csv file “FOOD.csv”,
also display number of food of this category.

Question 9:
A csv file “DOCTOR.csv” has structure [ Dname, Dept, Exp]
i. Write a user defined function add() to input data for a record and add to “DOCTOR.csv”.
ii. Write a function Count() in Python which count the number of doctor stored in the csv file “DOCTOR.csv”
ii. Write a function CountRec(Dept) in Python which accepts the department as parameter and count and display
doctor who are working in that department and having experience more than years, are stored in the csv file
“DOCTOR.csv” .

Question 10:
A csv file “ITEM.csv” has structure [ Iname, Cost_Price, Selling_Price]
i. Write a user defined function add() to input data for a record and add to “ITEM.csv”.
ii. Write a function CountRec(Selling_price) in Python which accepts the Selling_Pice as parameter and count and
return number of item whose selling price is more that price, are stored in the csv file “ITEM.csv” .
i. Write a function Countprt() in Python read contents of the file “ITEM.csv” and display details of item which are
sell on more than 25% profit. Also display number of items of this category.
Question 11:
A csv file “CLOTHS.csv” has structure [ Cid, Cname, Material, Gender]
i. Write a user defined function add() to input data for a record and add to “ITEM.csv”.
ii. Write a function Count(Material) in Python which accepts the Material as parameter and count and return
number of cloths of that material, are stored in the csv file “ITEM.csv” .
iii. Write a function countrec() in Python that would read contents of the file “CLOTHS.csv” and display the
details of cotton cloths for female. Also display number of cotton cloths for female.

You might also like