0% found this document useful (0 votes)
8 views

CSV File

Uploaded by

Defender xD
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CSV File

Uploaded by

Defender xD
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

2024

CSV File
shreya salhotra
11889
S. No. Question Page No.
Q1) WAP to create a csv file.
import csv

def createcsv():

f = open("student.csv", "w", newline = "")

c = csv.writer(f)

while True:

rno = int(input("Enter Roll No.:"))

name = input("Enter Name:")

per = float(input("Percentage:"))

line = [rno, name, per]

c.writerow(line)

ch = input("Enter n to terminate.")

if ch.lower() == "n":

break

f.close()

createcsv()

Q2) WAP to create a csv file and write multiple rows.


import csv

def createcsv():

f = open("student.csv", "w", newline = "")

c = csv.writer(f)

lines = []
while True:

rno = int(input("Enter Roll No.:"))

name = input("Enter Name:")

per = float(input("Percentage:"))

line = [rno, name, per]

lines.append(line)

ch = input("Enter n to terminate.")

if ch.lower() == "n":

break

c.writerows(lines)

f.close()

createcsv()

Q3) WAP to read and display contents of a CSV File.


import csv

def readfile():

f = open("student.csv", "r", newline = "")

c = csv.reader(f)

for i in c:

print(i)
f.close()

readfile()

Q4) Write the following functions:


a) Add_Device(): to accept a record from the user and add it to a csv file, peripheral.csv
b) Count_Device(): to count and display the number of peripheral devices whose price <
1000

peripheral.csv : [P_id, P_name, Price]


P_id = Peripheral deice ID (integer)
P_name = Peripheral device name (string)
Price = Peripheral device price (integer)

You might also like