python for循环打印列表字符而不是元素

时间:2016-04-25 03:34:02

标签: python

我有一个字典列表,当我想在字典中打印键的列表索引元素时,输出是字符的索引而不是元素。

reader = csv.DictReader(mycsvfile, fieldnames)
        for row in reader:
            print row

示例输出:

{'name':'tom','numbers':"['1','2','3']"} #row

print row ['numbers']

['1',2','3'] #correct

print row ['numbers'] [0]

[           #wrong

如何将row ['numbers']转换为列表?

1 个答案:

答案 0 :(得分:0)

没关系:

>>> row = {'name':'tom','numbers':['1','2','3']}
>>> row['numbers']
['1', '2', '3']
>>> row['numbers'][0]
'1'
>>> row['numbers'][1]
'2'
>>> row['numbers'][2]
'3'
>>>