'Fetch specific records from a csv file, based on user defined conditions
I have been trying to write a program, which will help the user see the details of a student by entering only an attendance id.
import csv
field = ["Roll no" , "Name" , "Class","Section"]
f = open("student.csv" , 'w')
d=csv.writer(f)
d.writerow(field)
ch='y'
while ch=='y' or ch=='Y':
gr=int(input("Enter GR number: "))
nm = input("Enter name:")
cls = input("Enter Class:")
sec = input("Enter section:")
rec=[gr,nm,cls,sec]
d.writerow(rec)
ch=input("Enter more record??(Y/N)")
f.close()
f=open("student.csv" , "r")
d=csv.reader(f)
next(f)
s=int(input("GR no of student whose details have to be displayed"))
print()
for i in d:
if i[0]==s:
print("grno=",i[0])
print("Name=", i[1])
print("class=", i[2])
print("section =",i[3])
print("--------------------")
f.close()
the error that i am getting:
Traceback (most recent call last):
File "C:/Python310/myfile8.py", line 25, in <module>
if i[0]==s:
IndexError: list index out of range
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|