0

我有这样的功能:

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

我怎样才能解决这个问题?谢谢

4

3 回答 3

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

这将返回一个包含用户名、密码的二维元组列表。

list = pass_uname_passwd()

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

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

于 2013-01-03T04:37:20.183 回答
1

我认为其他答案很好地涵盖了它。为了其他稍微不同的方式,您还可以使用生成器来生成yieldCSV 中的行:

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()on忽略标题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

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

于 2013-01-03T04:47:46.603 回答
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()
于 2013-01-03T04:44:05.343 回答