当前位置: 动力学知识库 > 问答 > 编程问答 >

python - TypeError csv.reader through a List

问题描述:

I create a list of filename titled "dict1". See a sample of the error message for what the list looks like.

Then I want to read through each filename in "dict1" until all have been processed.

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

Error Message:

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

I am not sure why I am getting that error. I just want to "append" the directory to each "row"(filename) in Dict1 and read through each one.

网友答案:

dict1 is a single string, not a list or dictionary. You are trying to use it as a function with dict1(row).

You probably just want to loop over the original list instead:

for row in list_words[0:5]:
    reader = csv.reader(open(dir + row))
分享给朋友:
您可能感兴趣的文章:
随机阅读: