Reputation: 13
I wan to print Student data and Class data. So, I wrote a code to fetch Student_id and Class_id.
Firstly the Input Student_id must match with the Stud_id in Student_data.csv file and print the data according.[I found the answer for this]
Samely, the input class_id must match with the Class_id in Class_data.csv file and print the data according.
I need the Final output as below[marked in **]:
Option 8: Enrol a student to a class Enter student id: S001 Enter class id: C001
Code:
if option == 8:
Stud_id = str(input("Enter Student Id : ")).lower()
Clas_id = str(input("Enter Class ID : ")).lower()
Stud_File=open('Student_Data.csv', "r") ## Reading CSV file.
Stud_Reader=list(csv.reader(Stud_File, delimiter=',')) ## Converting the csv file into List.
Class_File=open('Class_Data.csv', "r") ## Reading CSV file.
Class_Reader=list(csv.reader(Stud_File, delimiter=',')) ## Converting the csv file into List.
for row in Stud_Reader: ## Creating a for loop to search sub-string in a string in a CSV file data.
for field in row:
if Stud_id in field:
print("Student " +Stud_id), row[1],(" Enrolled into Class " +Clas_id)
for row in Class_Reader:
for field in row:
if Clas_id in field:
print(row[1], row[2])
Student S001, Kitty Tan enrolled into class C001, CCW101, Computer and Information Processing *
Upvotes: 1
Views: 109
Reputation: 11
if you give your sample csv, it will be faster to understand. Below will work for your problem.
def check_student_id(student_id):
try:
Stud_File = open('Student_Data.csv', "r")
Stud_Reader = list(csv.reader(Stud_File, delimiter=','))
for stud_id_row in Stud_Reader:
for stud_id in stud_id_row:
if str(stud_id).lower() == student_id:
return True
return False
except Exception as Err:
print(str(Err))
return False
def check_class_id(class_id):
try:
Class_File = open('Class_Data.csv', "r")
Class_Reader = list(csv.reader(Class_File, delimiter=','))
for class_id_row in Class_Reader:
for cls_id in class_id_row:
if str(cls_id).lower() == class_id:
return True
return False
except Exception as Err:
print(str(Err))
return False
Stud_id = str(input("Enter Student Id : ")).lower()
Clas_id = str(input("Enter Class ID : ")).lower()
is_student_id_exists = check_student_id(Stud_id)
if is_student_id_exists:
is_class_id_exits = check_class_id(Clas_id)
if is_class_id_exits:
print("Student {}, Enrolled into Class {}".format(Stud_id, Clas_id))
else:
print("Class id doesnt exits.")
else:
print("Student id doesnt exits.")
if you CSV have data of student ID and class registered then we need to retrieve that data to show. But what i assume for you question is "Need to validate student ID and class ID present before proceeding to enrolling"
Upvotes: 0
Reputation: 355
If you give the design of the .csv i guess you can use Pandas to make the access simpler. But i guess just store the string and print it later. and use break to end for when you found your student id.
Upvotes: 1
Reputation: 1215
Your code looks like it almost does what you want.. but you can store the student text "Student S001, Kitty Tan enrolled into class C001" in a variable and just print it later.. you could also try using end=""
in your first print
statement, which would stop it from printing a newline.. but I don't think that's as good a way to do this and it's only do-able in python3.
Stud_id = str(input("Enter Student Id : ")).lower()
Clas_id = str(input("Enter Class ID : ")).lower()
Stud_File=open('Student_Data.csv', "r") ## Reading CSV file.
Stud_Reader=list(csv.reader(Stud_File, delimiter=',')) ## Converting the csv file into List.
Class_File=open('Class_Data.csv', "r") ## Reading CSV file.
Class_Reader=list(csv.reader(Class_File, delimiter=',')) ## Converting the csv file into List.
for row in Stud_Reader: ## Creating a for loop to search sub-string in a string in a CSV file data.
for field in row:
if Stud_id in field:
student_text = "Student " + Stud_id + row[1] + " Enrolled into Class " + Clas_id
for row in Class_Reader:
for field in row:
if Clas_id in field:
print(student_text, row[1], row[2])
Upvotes: 0