python - 如何访问字典键内列表项中的元素?

标签 python json python-3.x

我目前有一个 JSON 文件,可以输出各种天气数据。我可以在一定程度上完善我想要的信息,但需要进一步完善它,但我无法做到。

我尝试过根据位置选择它,例如 [1],并且我还尝试过基于键访问数据,例如['weather'] 但在 Python 抛出错误之前我只能对其进行优化。

{'coord': {'lon': -1.98, 'lat': 50.72}, 'weather': [{'id': 803, 'main': 
'Clouds', 'description': 'broken clouds', 'icon': '04d'}], 'base': 
'stations', 'main': {'temp': 293.6, 'pressure': 1022, 'humidity': 64, 
'temp_min': 292.15, 'temp_max': 294.82}, 'visibility': 10000, 'wind': 
{'speed': 5.1, 'deg': 230}, 'clouds': {'all': 75}, 'dt': 1559214671, 
'sys': {'type': 1, 'id': 1401, 'message': 0.0072, 'country': 'GB', 
'sunrise': 1559188945, 'sunset': 1559246925}, 'timezone': 3600, 'id': 
2640101, 'name': 'Poole', 'cod': 200}

上面的代码是我必须使用的 JSON 文件,我想细化和提取例如“天气”中的“主要”值,所以我想提取字符串“云”,但我已经尝试过就像 dict['weather]['main'] 但它只是抛出一个错误。

寻求一些建议:D

最佳答案

weather 的值是一个列表,因此您需要 dict['weather'][0]['main']

此外,我建议不要调用特定的字典 dict,因为它隐藏了内置的 dict 函数。

关于python - 如何访问字典键内列表项中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56377837/

相关文章:

android - 使用 JsonStore 在 kivy android 中保存变量

python - 提交按钮麻烦

jquery - 缓存自动完成 JSON 响应

ios - 使用 Objective C 转换 JSON 和构建字典

python - 使用 python3.7 修剪列表中的链接

python - 为什么我要从网页中提取的数据不在我的汤页面中?

python - 从类、函数、方法、模块打印文档字符串

ios - 无限登录循环?

python - Argparse 可选 bool 值

python - 如何解压 Pandas 数据框中的列表列