【问题标题】:How do I put given data into dictionary?如何将给定的数据放入字典?
【发布时间】:2017-09-27 13:26:49
【问题描述】:

所以我有一些从网站到使用 json 的变量的行,代码如下所示:

with urllib.request.urlopen("someurl") as url:
    data = json.loads(url.read().decode())

现在,该数据包含如下文本:

“狗”:“汪”

“猫”:“喵”

等等..我如何放置这些数据,以便字典的键是动物,值是声音?

【问题讨论】:

  • 如果你使用requests,你可以直接使用import requests; data = requests.get(url).json()

标签: python json python-3.x dictionary


【解决方案1】:

json.loads() 已经为您做到了。尝试访问数据['Dog'] 你会得到'Woof'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    相关资源
    最近更新 更多