0

我有这段代码,并且根据我的脚本在 .csv 中的结果(重复打印了一个 UserID),我假设我的变量被覆盖并在每次迭代时重新分配。

# User_alias is a raw input (of multiple items)

    print user_alias
        for user in user_alias:
            for row in Dict_list:    
                if user in row.values():
                    print row

        for user in user_alias:
            for row in Dict_list:
                if row.has_key('_akq6p') and row.has_key('_cn6ca'):
                    if user == row['_akq6p'] or user == row['_chk2m']:


#EDIT:
                    UserID = row['_cn6ca']
                    UserIDs=[]
                    UserIDs.append(row['_cn6ca'])
                    print UserIDs

这是一个示例输入/输出:

Raw input:
Adue Sal
Arthur Bel
Abby Sue

['Adue Sal', 'Art Baraan', 'Abbey Shi']
{'_akq6p': 'adkul', '_dcgjs': 'adkul', '_chk2m': 'Adue Sal', '_cn6ca': '1200df00'}
{'_akq6p': 'artb', '_dcgjs': 'artb', '_chk2m': 'Arthur Bel', '_cn6ca': '00123ffj8'}
{'_akq6p': 'abbys', '_dcgjs': 'abbys', '_chk2m': 'Abby Sue', '_cn6ca': '125gh789'}
['1200df00']
['00123ffj8']
['125gh789']
logout

[Process completed]

我需要将这些写入 .csv,因此我需要存储每个 ID,但此代码似乎只存储查找的最后一个 ID。我将如何存储所有这些 ID 以供以后编写?

4

1 回答 1

0

这是我跑的:http ://codecube.io/TlFgaY

您需要在循环之外创建列表,然后添加到它。

print user_alias
for user in user_alias:
    for row in Dict_list:    
        if user in row.values():
            print row

UserIDs = [] # This is the change.
for user in user_alias:
    for row in Dict_list:
        if row.has_key('_akq6p') and row.has_key('_cn6ca'):
            if user == row['_akq6p'] or user == row['_chk2m']:
                UserID = row['_cn6ca']
                print UserID
                UserIDs.append(UserID)

print UserIDs
于 2013-10-21T16:40:26.420 回答