我的csv文件是

200
Service

我在解释器中输入的代码是
snav = csv.DictReader(open("screennavigation.csv"), delimiter=',')
print snav.fieldnames
['200']

for line in snav:
...     print(line)
...
{'200': 'Service'}

snav["200"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: DictReader instance has no attribute '__getitem__'

我以为DictReader是用来归还字典的。我怀疑我遗漏了一些显而易见的东西。

最佳答案

snav对象是DictReader实例,不应作为字典访问在迭代过程中,它生成可相应访问的字典:您需要line['200']

10-04 11:43