通过List键入TypeError csv.reader

时间:2014-05-01 20:41:27

标签: python list loops csv

我创建了一个名为“dict1”的文件名列表。请参阅错误消息示例,了解列表的外观。

然后我想通读“dict1”中的每个文件名,直到所有文件都被处理完毕。

# creates dict each containing 5 file names to be processed.
list = open("/opt/stateadm/AAM_Scripts/Realtime/file.txt").read()
list_words = list.split('\n')
dict1 = ('\n'.join(list_words[0:5]))
print dict1
# for each row in dict (file) open read and publish to Redis
for row in dict1:
    dir = '/opt/stateadm/AAM_Scripts/Realtime/outgoing/traits/'
    reader = csv.reader(open(dir + dict1(row)))
    header = reader.next()
    client = redis.StrictRedis(host='XXXXXXXXXXXXXXXXXXXXXXX', port=6379, db=0)
    client1 = redis.StrictRedis(host='XXXXXXXXXXXXXXXXXXXXXXX', port=6379, db=0)
    client2 = redis.StrictRedis(host='XXXXXXXXXXXXXXXXXXXXXXX', port=6379, db=0)
    list1 =(client, client1, client2)
    for row in reader:
        key = 'AAM_CDF_Traits'
        doc = dict(zip(header, row))
        random.choice(list1).publish(key, doc)

错误讯息:

print dict1
split_bj.csv
split_bi.csv
split_bh.csv
split_bg.csv
split_bf.csv

Traceback (most recent call last):
  File "./AAM_Redis6.sh", line 32, in <module>
    reader = csv.reader(open(dir + dict1(row)))
TypeError: 'str' object is not callable

我不知道为什么我会收到这个错误。我只想将目录“附加”到Dict1中的每个“行”(文件名),并通读每个目录。

1 个答案:

答案 0 :(得分:0)

dict1是单个字符串,而不是列表或字典。您正尝试将其用作dict1(row)的函数。

您可能只想循环遍历原始列表:

for row in list_words[0:5]:
    reader = csv.reader(open(dir + row))