0

Hey I am trying to read data from a list that is from a CSV file

def Load(): #Loads data from the csv that can be stored in functions
global userdata
global user
userdata = []
f = open('userdata.csv','r')
data = csv.reader(f)
for row in data:
    user = []
    for field in row:
        user.append(field)
    userdata.append(user)
f.close()

This is the login function which I am looping over

def Login(): #Login function
global userdata
Load()
global user
print('Please now login to your account')

x = False
while x == False:
    usernameLog = input('Please enter your username: ')

    j = len(userdata)
    for i in range(0,j):
        if usernameLog == userdata [i][0]: #Validates username
            print('Username accepted')

            time.sleep(1)

My program successfully writes to the CSV but just doesn't read from it without throwing out this error. I might just be being stupid though.

5
  • 1
    On which line is this error thrown? Commented Mar 9, 2018 at 9:26
  • On line 127 IF usernameLog == userdata [i][0]: @cᴏʟᴅsᴘᴇᴇᴅ Commented Mar 9, 2018 at 9:27
  • could you please also add a example of the csv? Commented Mar 9, 2018 at 9:34
  • Do you mean like: Username: bob and Password: password1? Commented Mar 9, 2018 at 9:36
  • 1
    The first item in the list is the Username and the second is the password @DanielRodríguez Commented Mar 9, 2018 at 9:37

1 Answer 1

2

You have the line user = [] inside the for loop, so you are always "cleaning" user before appending the new value, so only the last value is added, and the previous one is removed.

You should take it out of the loop, the same you are doing with userdata.

(This is what it looks like, unless your csv structure is totally different and you don't need one user per one userdata)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that worked. I didn't think it would do that :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.