Python CSV file input, validation not working

I tried to print out the variable gayle and it assigns values correctly. I can’t find why is my program stopping if i input non-existent ID.

    os.system("clear")
    print('tSEARCH AND VIEW FILEn')
    gayle=0

    file_input = input('Search ID Number: ')
    proj = io.open('all_projects.csv', 'r')        
    while True:
        data = proj.readline()
        if file_input in data:
            txt = data
            gayle += 1
            break
    proj.close()
    
    if gayle > 0:
        list2 = txt.split(",")
        i = Preview(list2)
        i.view()
        print(gayle)
        try_again('Search file again?','Invalid input.',2)
    elif (gayle == 0):
        print("Not exist")

Answer

You need to only loop over lines in your file you can do this using the open() and readlines() functions. Change your file reading and loop to the following and it should work

proj = open('all_projects.csv', 'r')
Lines = proj.readlines()
proj.close()

for line in Lines:
    if file_input in line:
            txt = data
            gayle += 1
            break

Leave a Reply

Your email address will not be published. Required fields are marked *