Grade 12 CS Board Practicals CSV

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

PART I Python Programming : 08 marks

Logic 5 marks, Documentation 1 ½ marks and Code quality 1 ½ marks


PART II MYSQL : 04 marks
PART III Report File : 07 marks
PART IV Project : 08 marks
PART V Viva Voce : 03 marks
CSV FILE
SET 2
PART I : LAB TEST- 1: (Python Programming: 8 marks)
Logic: 5 marks Documentation: 1 ½ marks Code quality: 1 ½ marks
Define the following user defined functions to operate on a CSV File:
a) def Writeinfo() : To insert a record into a csv file named “Tourist.csv” (Ensure
that the header row is written only once into a file) with the following details:
Slno, Tourist Name, City
b) def Readinfo() : To display all the records stored in the csv file “Tourist.csv”.

Create a Menu driven program that invokes the required function defined above to execute the
task chosen by the user. The program should execute as long as the user wants. Also Handle
all file based exceptions/errors.

MENU:
1. Insert a New Record (Retain previous data)
2. View all Records
3. Exit
import csv
def Writeinfo():
fobj=open("Tourist.csv" , "a+", newline='')
wobj=csv.writer(fobj)
header=['Slno', ' Tourist Name', 'City']
if fobj.tell()==0:
wobj.writerow(header)
slno=int(input("Enter the serial number: "))
tname=input("Enter the tourist name: ")
city=input("Enter the city: ")
rec=[slno,tname, city]
wobj.writerow(rec)
fobj.close()

def Readinfo():
try:
fobj=open("Tourist.csv" , "r", newline="")
robj=csv.reader(fobj)
for rec in robj:
print(rec)
fobj.close()
except FileNotFoundError:
print("File Not Found")

while True:
print("1 to insert a new record")
print("2 to view all records")
print("3 to exit")
choice=int(input("Enter your choice: "))
if choice==1:
Writeinfo()
if choice==2:
Readinfo()
if choice==3:
break

SET 8
PART I : LAB TEST- 1: (Python Programming: 8 marks)
Logic: 5 marks Documentation: 1 ½ marks Code quality: 1 ½ marks

Using user defined functions, write a Menu Based Program to perform the following
operations on a csv file called “students.csv”.
1. Add a new Record. (Retain previous data)
2. Display all Records.
3. Display Records of those students whose age is between 10 and 15.
4. Exit
Each record has the following structure:
[student id, student name, age]
The program should execute as long as the user wants. Also Handle all file-based
exceptions/errors
import csv
def Writeinfo():
fobj=open("students.csv" , "a", newline='')
wobj=csv.writer(fobj)
header=['student id', 'student name', 'age']
if fobj.tell()==0:
wobj.writerow(header)
student_id=int(input("Enter the student id: "))
student_name=input("Enter the student name: ")
age=int(input("Enter the age: "))
rec=[student_id, student_name, age]
wobj.writerow(rec)
fobj.close()

def Readinfo():
try:
fobj=open("students.csv" , "r", newline="")
robj=csv.reader(fobj)
for rec in robj:
print(rec[0],rec[1],rec[2])
fobj.close()
except FileNotFoundError:
print("File Not Found")

while True:
print("1 to insert a new record")
print("2 to view all records")
print("3 to exit")
choice=int(input("Enter your choice: "))
if choice==1:
Writeinfo()
if choice==2:
Readinfo()
if choice==3:
break

SET 12

PART I : LAB TEST- 1: (Python Programming: 8 marks)


Logic: 5 marks Documentation: 1 ½ marks Code quality: 1 ½ marks

Using user defined functions, write a Menu Based Program to perform the following
operations on a csv file called “product.csv”.
1. Add a new Record. (Retain previous data)
2. Display all Records.
3. Compute and display product id, price and 2% hike in price
4. Exit
Each record has the following structure:
[productid, product name, price]
The program should execute as long as the user wants. Also Handle all file-based
exceptions/errors.

import csv
def writeinfo():
fobj=open("product.csv" , "a", newline='')
wobj=csv.writer(fobj)
header=['productid', ' product name', 'price']
fobj.tell()==0:
wobj.writerow(header)
prodid=int(input("Enter the product ID: "))
pname=input("Enter the product name: ")
price=input("Enter the price: ")
rec=[prodid,pname, price]
wobj.writerow(rec)
fobj.close()

def readinfo():
try:
fobj = open("product.csv", "r", newline="")
robj = csv.reader(fobj)
for rec in robj:
print(rec)
fobj.close()
except FileNotFoundError:
print("File Not Found Error")

def compute():
try:
fobj = open("product.csv", "r", newline="")
robj = csv.reader(fobj)
next(robj) #to skip the header row
print(['Product','Price','Hiked Price'])
for rec in robj:

print([rec[1],rec[2],(int(rec[2])+(int(rec[2])*0.02))])
fobj.close()
except FileNotFoundError:
print("File Not Found Error")

while True:
print('''1. Add a new Record. (Retain previous data)
2. Display all Records.
3. Compute and display product id, price and 2% hike in price
4. Exit''')
choice=int(input("Enter your choice: "))
if choice==1:
writeinfo()
elif choice==2:
readinfo()
elif choice==3:
compute()
elif choice==4:
break
else:
print("Invalid Input! Try Again!")
SET 14
PART I : LAB TEST- 1: (Python Programming: 8 marks)
Logic: 5 marks Documentation: 1 ½ marks Code quality: 1 ½ marks

Using user defined functions, write a Menu Based Program to perform the following
operations on a csv file called “Temperature.csv”.
1. Add a new Record. (Retain previous data)
2. Display all Records.
3. Compute and display city name and Average Temperature in Fahrenheit
4. Exit
Each record has the following structure:
City name , Average Temperature in Celsius
The program should execute as long as the user wants. Also Handle all file-based exceptions /
errors.
9
Note: 𝐹𝑎ℎ𝑟𝑒𝑛ℎ𝑒𝑖𝑡 = 𝐶𝑒𝑙𝑠𝑖𝑢𝑠 × + 32
5
import csv
def writeinfo():
fobj=open("Tourist.csv" , "a+", newline='')
wobj=csv.writer(fobj)
if fobj.tell()==0:
wobj.writerow(["City name" , "Average Temperature in Celsius"])
cityname=input("Enter the city: ")
avg_temp=float(input("Average Temperature in Celsius: "))
rec=[cityname,avg_temp]
wobj.writerow(rec)
fobj.close()

def readinfo():
try:
fobj=open("Tourist.csv" , "r", newline="")
robj=csv.reader(fobj)
for rec in robj:
print(rec)
fobj.close()
except FileNotFoundError:
print("Tourist.csv does not exist")

def Compute():
try:
fobj=open("Tourist.csv" , "r", newline="")
robj=csv.reader(fobj)
next(robj)
print("________________________________________________")
print("City name" , "Average Temperature in Fahrenheit")
for rec in robj:
#Fahrenheit =Celsius × 9/5+32
Fah=float(rec[1])*9/5+32
print("________________________________________________")
print(rec[0],Fah)
fobj.close()
except FileNotFoundError:
print("Tourist.csv does not exist")

while True:
choice=int(input("""Enter your choice
1 To insert a new record
2 To view all records
3 To Compute and display city name and Average Temperature in Fahrenheit
4 To exit"""))

if choice==1:
writeinfo()
elif choice==2:
readinfo()
elif choice==3:
Compute()
elif choice==4:
break
else:
print("Invalid Input")

You might also like