Python CSV: Read and Write CSV Files
Python CSV: Read and Write CSV Files class 12
How do I search for a record in a CSV file?
How to find the minimum and maximum value of a column in Python
Program for write, read, search, find maximum number, find minimum number in csv file class 12
#Program for writing in csv file
import csv
def write():
with open("ram.csv","w",newline='') as fobj:
f=csv.writer(fobj)
data=['roll','name','marks']
f.writerow(data)
while True:
roll=int(input("Enter the roll no:"))
name=input("Enter the Name:")
marks=int(input("Enter the Marks:"))
data2=[roll,name,marks]
f.writerow(data2)
key=int(input("Press 1-> for Continue: \n Press 2-> for Stop:"))
if key==2:
break
#Program for reading in csv file
def read():
with open ("ram.csv","r")as fobj:
f=csv.reader(fobj)
for i in f:
print(i)
#Program for Searching Record in csv file
def search():
with open ("ram.csv","r")as fobj:
f=csv.reader(fobj)
flag=0
roll=int(input("Enter the roll no:"))
next(f)
for i in f:
if int(i[0])==roll:
flag=1
print("------------Founded Record is----------\n",i)
break
if flag==0:
print("Record not found...............")
#Program for maximum number in csv file
def maxmarks():
with open ("ram.csv","r")as fobj:
f=csv.reader(fobj)
maxm=-1
next(f)
for i in f:
if int(i[2])>maxm:
maxm=int(i[2])
z=i
print("-------The Student with Maximum Marks is-------\n",z)
#Program for minimum number in csv file
def minmmarks():
with open("ram.csv","r") as fobj:
f=csv.reader(fobj)
minm=101
next(f)
for i in f:
if int(i[2])<minm:
minm=int(i[2])
z=i
print("-------The Student with minmmarks is-------\n",z)
#calling function here
write()
read()
search()
maxmarks()
minmmarks()
No comments:
Post a Comment
for more information please share like comment and subscribe