如何在Python中循环使用此列表中的用户名和密码?

时间:2013-01-03 04:33:44

标签: python

我有这样的功能:

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()

输出是:

[['admin@gmail.com', '1234'], ['ok@gmail.com', 'metal'], ['hithere@gmail.com', 'hello']]

我想循环每个用户名和密码,直到列表末尾。目的是我想逐个传递用户名和密码。 首先,我想使用admin@gmail.com和1234,然后继续使用另一个。

我尝试使用:

    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     

但是这会立即返回用户名和密码,如下所示:

admin@gmail.com
ok@gmail.com
hihere@gmail.com
1234
metal
hello

我该如何解决这个问题?感谢

3 个答案:

答案 0 :(得分:1)

将此更改为单个功能:

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

这将返回包含用户名,密码的2d元组列表。

list = pass_uname_passwd()

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

您也可以在第一个函数中执行此操作,之后使用for循环。我建议的唯一变化是使用元组而不是列表,以使数据不可变。

答案 1 :(得分:1)

我认为其他答案很好。为了其他略有不同的方式,您还可以使用生成器{CSV}中的yield行:

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
   ...:     
   ...:     
admin@gmail.com 1234
ok@gmail.com metal
hithere@gmail.com hello

此外,如果你的问题更多'如何解析我回来的列表?',你可以这样做:

In [8]: l = [['admin@gmail.com', '1234'], ['ok@gmail.com', 'metal'], ['hithere@gmail.com', 'hello']]

In [9]: for info in l:
   ...:     uname, passwrd = info
   ...:     print uname, passwrd
   ...:     
   ...:     
admin@gmail.com 1234
ok@gmail.com metal
hithere@gmail.com hello

另外,根据下面的评论,您可以使用next()上的csv_file来忽略标题,这会使迭代前进一步,这意味着您的for row in csv_file循环开始于CSV的第二行(注意:DictReader是此处的另一个选项,其中每一行都以{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

上面的输出将是相同的,减去第一行。

答案 2 :(得分:0)

一种方法:

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)

或者更直截了当的方式:

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()