Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am doing a project for a university course to create a banking app using python. This requires a login and the data to be stored in a CSV.
The issue I have is to iterate through the 2D array that has data from the csv.
Through my debugging the iteration is stopping before the first row and can't seem to figure out how to resolve this.

What I have tried:

This is the current code I have since it is a function. where it has "print(Username)" was only there for testing purposes.

Python
# Function to select which account is being accessed from Accounts.CSV.
def account_select(Username):
    print(Username)
    accounts = open('Accounts.csv', 'r')
    print(accounts.read())
    print(Username, "1")
    for row in accounts:
        print(Username, "2")
        for column in row:
            print(Username, "3")
            if column[3] == Username:

                print("Username Match")
            if column[3] != Username:
                print("Error Occurred")
Posted
Updated 9-Jun-20 10:07am
v2

1 solution

Python
print(accounts.read()) # this reads all the lines from your file, but you don't save the result
print(Username, "1")
for row in accounts: # there is nothing to read now, since it has all been read above

Remove the line print(accounts.read()) and it should work.

Also, in the following, why not just use else for the second part:
Python
if column[3] == Username:

    print("Username Match")
# if column[3] != Username: this is redundant
# if it does not equal the comparison value then it must be not equal, just use else 
else: 
    print("Error Occurred")
 
Share this answer
 
v3
Comments
Afzaal Ahmad Zeeshan 9-Jun-20 20:35pm    
5ed.
Richard MacCutchan 10-Jun-20 2:47am    
Thanks Afzaal. Haven't seen you here for a while, have you been working?
Afzaal Ahmad Zeeshan 10-Jun-20 16:30pm    
Hey Richard!

Yes, I started working on a few projects with some clients. What has been taking most of my time is my studies—I started my MS in Computer Science.

I am planning to join the community again, but you know how assignments are. :laugh:

Also, I hope you are safe and healthy during this COVID-19.
Richard MacCutchan 11-Jun-20 4:17am    
Good luck with your degree. We are safe here although missing regular contact with our grandchildren. I am not sure how good or bad things have been in Pakistan, but I hope you are keeping well also.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900