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

CSV File

Uploaded by

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

CSV File

Uploaded by

aaditya160807
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSV FILE

Source code:
#To perform different operations in csv file
import csv
#function to write in csv file.
def createCSV1():
csvfile=open('student.csv','w',newline='' )
csvobj=csv.writer(csvfile)
while True:
r=int(input("enter rollno: "))
n=input("enter name: ")
m=int(input("enter marks: "))
l=[r,n,m]
csvobj.writerow(l)
ch=input('do you want to enter more(y/n)? ')
if ch in ('Nn'):
break
csvfile.close()
#using Writerows() fn to write multiple record in a csv file.
def createCSV2():
csvfile=open('student.csv','a',newline='' )
csvobj=csv.writer(csvfile)
l1=[]
while True:
r=int(input("enter rollno: "))
n=input("enter name: ")
m=int(input("enter marks: "))
l=[r,n,m]
l1.append(l)
ch=input('do you want to enter more(y/n)? ')
if ch in ('Nn'):
break
csvobj.writerows(l1)
csvfile.close()
#to read and show the content of a csv file.
def showall():
csvfile=open('student.csv','r',newline='')
csvobj=csv.reader(csvfile)
for line in csvobj:
print(line)
csvfile.close()
#MENU
while True:
print()
op=int(input("""
1.Create csv file
2.Create csv file by using writerows() fn
3.display csv file
4.Quit
enter your choice :"""))
if op==1:
createCSV1()
elif op==2:
createCSV2()
elif op==3:
showall()
elif op==4:
print("Exiting....")
break
else:
Print("invalid choice")

OUTPUT:
1.Create csv file
2.Create csv file by using writerows() fn
3.display csv file
4.Quit
enter your choice :1

enter rollno: 1
enter name: abhishek
enter marks: 19
do you want to enter more(y/n)? y
enter rollno: 2
enter name: chitra
enter marks: 12
do you want to enter more(y/n)? y
enter rollno: 3
enter name: kavish
enter marks: 7
do you want to enter more(y/n)? y
enter rollno: 5
enter name: prachi
enter marks: 9
do you want to enter more(y/n)? y
enter rollno: 22
enter name: ravi
enter marks: 0
do you want to enter more(y/n)? y
enter rollno: 15
enter name: sorav
enter marks: 19
do you want to enter more(y/n)? n

1.Create csv file


2.Create csv file by using writerows() fn
3.display csv file
4.Quit
enter your choice :2
enter rollno: 29
enter name: manav
enter marks: 11
do you want to enter more(y/n)? y
enter rollno: 30
enter name: pragati
enter marks: 8
do you want to enter more(y/n)? y
enter rollno: 32
enter name: rashma
enter marks: 17
do you want to enter more(y/n)? y
enter rollno: 36
enter name: yonit
enter marks: 14
do you want to enter more(y/n)? n

1.Create csv file


2.Create csv file by using writerows() fn
3.display csv file
4.Quit
enter your choice :3
['1', 'abhishek', '19']
['2', 'chitra', '12']
['3', 'kavish', '7']
['5', 'prachi', '9']
['22', 'ravi', '0']
['15', 'sorav', '19']
['29', 'manav', '11']
['30', 'pragati', '8']
['32', 'rashma', '17']
['36', 'yonit', '14']

1.Create csv file


2.Create csv file by using writerows() fn
3.display csv file
4.Quit
enter your choice :4
Exiting....

You might also like