【问题标题】:Why are "dictionary" and "keys" not defined in python?为什么python中没有定义“字典”和“键”?
【发布时间】:2012-04-11 14:43:51
【问题描述】:

我很擅长 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 具有类型dict,因为我使用了print type(item)。但是当我尝试运行上面的代码时,我得到NameError: name 'dictionary' is not defined——我需要导入一个定义字典函数的库吗?如果有,是哪个库?

【问题讨论】:

  • 只是entry.keys()

标签: python json


【解决方案1】:

print entry.keys()

而不是

print dictionary.keys(entry)

我建议您看看tutoriallibrary reference

【讨论】:

  • 其实我需要item.keys();感谢教程链接。我已经阅读了很多,但没有找到教程。
  • @Amanda:很可能你需要item.keys()。这取决于data 的结构和其中的内容。
猜你喜欢
  • 2018-08-21
  • 2014-01-13
  • 2022-11-29
  • 2022-07-05
  • 1970-01-01
  • 2013-08-31
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
相关资源
最近更新 更多