[go: up one dir, main page]

0% found this document useful (0 votes)
78 views4 pages

CSV File Operations in Python

This document contains Python code to handle CSV files. It defines functions to add, modify, search, delete and view records in a CSV file. The main menu function calls these functions and allows the user to choose an operation.

Uploaded by

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

CSV File Operations in Python

This document contains Python code to handle CSV files. It defines functions to add, modify, search, delete and view records in a CSV file. The main menu function calls these functions and allows the user to choose an operation.

Uploaded by

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

CSV File Handling

import os
import csv
def addrecord():
print("ADDING new record to CSV file")
fp=open("Stu.csv", 'a',newline="\r\n")
cw=csv.writer(fp)
rollno=int(input("Enter Rollno"))
name=(input("Enter Name of student")
marks=float((input("Enter Marks"))
rec=[rollno,name,marks]
cw.writerow(rec)
f.close()
print("Record written to CSV file")
input("Press any key to continue")
def modifyrecord():
print("Modifying record in CSV file")
fp=open("Stu.csv", 'a', newline="\n")
fp1=open("Temp.csv", 'a', newline="\n")
cr= csv.reader(fp)
cw1=csv.writer(fp1)
rno=int(input("Enter Rollno you want to modify"))
for rec in cr:
if int( rec[0])==rno:
print("Rollno=", rec[0])
print("Name=", rec[1])
print("Marks=", rec[2])
choice=input("Do you want to modify this record(y/n)")
if choice=="y" or choice == "Y":
rollno=int(input("Enter new Rollno"))
name=(input("Enter new Name of student")
marks=float((input("Enter new Marks"))
rec=[rollno,name,marks]
cw1.writerow(rec)
print("Record written to CSV file")
print("Record modified to CSV file")
else:
cw1.writerow(rec)
else:
cw1.writerow(rec)

fp.close()
fp1.close()
os.remove("stu.csv")
os.rename("temp.csv", "stu.csv")
input("Press any key to continue")
def deleterecord():
print("Deleting record in CSV file")
fp=open("Stu.csv", 'a', newline="\r\n")
fp1=open("Temp.csv", 'a', newline="\r\n")
cr= csv.reader(fp)
cw1=csv.writer(fp1)
rno=int(input("Enter Rollno you want to delete"))
for rec in cr:
if int( rec[0])==rno:
print("Rollno=", rec[0])
print("Name=", rec[1])
print("Marks=", rec[2])
choice=input("Do you want to delete this record(y/n)")
if choice=="y" or choice == "Y":
pass
print("Record deleted")
else:
cw1.writerow(rec)
fp.close()
fp1.close()
os.remove("stu.csv")
os.rename("temp.csv", "stu.csv")
input("Press any key to continue")
def viewall(): #Read operation in CSV file
print("All Records in CSV File are as follows::::::")
fp=open("Stu.csv", 'a',newline="\r\n") #Remove newline char from output
cr= csv.reader(fp)
for rec in cr:
print("Rollno=", rec[0], end=" ")
print("Name=", rec[1] ], end=" ")
print("Marks=", rec[2], ], end=" ")
fp.close()
input("Press any key to continue")
def search():
print("Searching record in CSV File")
fp=open("Stu.csv", 'a', newline="\n")
cr= csv.reader(fp)
rno=int(input("Enter Rollno you want to Search"))
found=0
for rec in cr:
if int( rec[0])==rno:
print("Record found")
print("Rollno=", rec[0])
print("Name=", rec[1])
print("Marks=", rec[2])
found=1
if found==0:
print("Record not found in CSV file")
f.close()
input("Press any key to continue")
def mainmenu():
choice=0
while choice !=6:
print("Main Menu")
print(" 1-> Add a new record")
print(" 2-> Modify existing record")
print(" 3-> Search a record")
print(" 4-> Delete a record")
print(" 5-> View or Display all records")
print(" 6-> Exit from program")
choice=int(input("Enter your choice between 1 to 6"))
if choice==1:
addrecord()

elif choice==2:
modifyrecord()
elif choice==3:
search()
elif choice==4:
deleterecord()
elif choice==5:
viewall()
elif choice==6:
print("Exiting from program")
os.exit()
else:
print("Invalid Choice")
break
mainmenu() # Function call

You might also like