为什么python中没有定义“字典”和“键”?

时间:2012-04-11 14:43:51

标签: python json

我很擅长使用PHP并尝试学习Python。所以请原谅我,如果我试图按字面意思翻译函数,但这就是我所拥有的:

import json
import urllib


url='http://www.example.com/sample.json'
f = urllib.urlopen(url)
data = json.loads(f.read())


for item in data:
    for entry in item:
        print dictionary.keys(entry)

我知道item有类型字典,因为我做了print type(item)。但是当我尝试运行上面的代码时,我得到NameError: name 'dictionary' is not defined - 我是否需要导入一个定义字典函数的库?如果是这样,哪个库?

1 个答案:

答案 0 :(得分:9)

print entry.keys()

而不是

print dictionary.keys(entry)

我建议您查看tutoriallibrary reference

相关问题