reading a csv file to log in

Asked by Jeff

Hi there,
 I have a csv file that looks like this

username1, password1
username2,password2
username3,password3

my script does this

import csv
fname="username.csv"
f = open(fname)
counter = 0
for line in f.readlines():
    email, password = line.strip().split(",")
    counter +=1
    print email, password, counter

# script then attempts to log in using the first username and password
# Do Stuff
# Logout and hen log in as second user and repeat
#Logout and then log in as third user and repeat
#logout

f.close()

What happens is when I run the script it does this

username1 password1 1
username2 password2 2
username3 password3 3

Then it goes to log in but it logs in as the third user and not the first... I know this is a simple mistake that I am making as I am a novice.

What I am expecting to happen is

1. Open file
2. Read first row of file
3. Login and perform tasks
4. logout
5. Read second row of file
6. Login and perform tasks
7. logout
8. Read third row of file
9. Login and perform tasks
10. logout

Thank you !

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Jeff
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

in your case, after the loop email and password contain the 3rd line content.

you have to put the login/logout action into the loop after the print:

for line in f.readlines():
    email, password = line.strip().split(",")
    counter +=1
    print email, password, counter
    doLogin(email, password)
    doLogout()

... assuming, that doLogin and doLogout are functions defined before, that do the job or are replaced by the lines of code representing the login and logout logic respectively.

Revision history for this message
Jeff (jxmccall) said :
#2

Hi RaiMan,
 Thank you for responding, That is indeed how my code is, however when it gets to the point of doLogin(email,password) code it uses the last values in the file, not the current row. So for the first pass it reads the file and all the usernames and passwords, it goes to the login and types the username from the last row in the file not the first or second, its always the last row in the file. I would expect that the first time I run the script it would read the values, pass the first row..do something, logout, read the second row, do something.....etc.

Revision history for this message
Jeff (jxmccall) said :
#3

Hi RaiMan,
 I found the issue. All the code in the for loop was not indented, therefore it did not see it as part of the loop ( I am assuming). Once I indented all the code the file was read line by line correctly and all my actions were performed.

Thank you for your help !

Revision history for this message
RaiMan (raimund-hocke) said :
#4

yes, correct indentation is crucial in Python language ;-)