user1881957
user1881957

Reputation: 3378

How to loop over username and password from this list in Python?

I have a function like this:

import csv 

def total():
    with open('test.csv', 'r') as csvfile:
        spamreader = csv.reader(csvfile)
        a = [row for row in spamreader]
        return a 
print total()

The output of is:

[['[email protected]', '1234'], ['[email protected]', 'metal'], ['[email protected]', 'hello']]

I want to loop over each username and password until the end of list. The purpose is I want to pass the username and password one by one. First I want to use [email protected] and 1234 and then proceed to another.

I tried using:

    def pass_username():
        with open('test.csv', 'r') as csvfile:
            spamreader = csv.reader(csvfile, delimiter=',')
            a = [row[0] for row in spamreader]
            return a 

def pass_password():
    with open('test.csv', 'r') as csvfile:
        spamreader = csv.reader(csvfile, delimiter=',')
        a = [row[1] for row in spamreader]
        return a     

But this returns username at once and password at once like this:

[email protected]
[email protected]
[email protected]
1234
metal
hello

How can I fix this? Thanks

Upvotes: 0

Views: 956

Answers (3)

RocketDonkey
RocketDonkey

Reputation: 37259

I think the other answers cover it well. In the interests of other slightly different ways, you can also use a generator to yield the rows from the CSV:

In [1]: import csv

In [2]: def unames_passwd():
   ...:     with open('test.csv', 'r') as f:
   ...:         reader = csv.reader(f)
   ...:         for row in reader:
   ...:             yield row
   ...:             
   ...:             

In [3]: for info in unames_passwd():
   ...:     uname, passwd = info
   ...:     print uname, passwd
   ...:     
   ...:     
[email protected] 1234
[email protected] metal
[email protected] hello

Also, in case your question is more 'How do I parse the list I get back?', you can do this:

In [8]: l = [['[email protected]', '1234'], ['[email protected]', 'metal'], ['[email protected]', 'hello']]

In [9]: for info in l:
   ...:     uname, passwrd = info
   ...:     print uname, passwrd
   ...:     
   ...:     
[email protected] 1234
[email protected] metal
[email protected] hello

Also, per your comment below, you can ignore headers by using next() on the csv_file, which will advance the iteration one step, meaning that your for row in csv_file loop with start on the second row of the CSV (note: DictReader is another option here, where each row is returned as a dictionary of {column_header: value_at_row}:

In [2]: def unames_passwd():
   ...:     with open('test.csv', 'r') as f:
   ...:         reader = csv.reader(f)
   ...:         next(reader)
   ...:         for row in reader:
   ...:             yield row

And the output from above would be the same, minus the first row.

Upvotes: 1

asheeshr
asheeshr

Reputation: 4114

Change this to a single function :

def pass_uname_passwd :
    with open('test.csv', 'r') as csvfile:
                spamreader = csv.reader(csvfile, delimiter=',')
                a = [(row[0], row[1]) for row in spamreader] #Make it a list of 2d tuples
                return a

This will return a list of 2d tuples containing username, password.

list = pass_uname_passwd()

for i in list :
#Use as desired, processing each (uname,passwd) at a time

You can simply do this from your first function as well, by using a for loop after that. The only change i suggest is using tuples instead of lists, to make the data immutable.

Upvotes: 1

Lie Ryan
Lie Ryan

Reputation: 64837

One way to do this:

def total(f):
    with open('test.csv', 'r') as csvfile: 
        spamreader = csv.reader(csvfile) 
        a = [f(row) for row in spamreader]
        return a 
def func(username, password):
    print username, password
print total(func)

Or a more straightforward way:

def total():
    with open('test.csv', 'r') as csvfile: 
        spamreader = csv.reader(csvfile) 
        for username, password in spamreader:
            print username, password
        return a 
print total()

Upvotes: 0

Related Questions