簡體   English   中英

如何訪問字典鍵中列表項中的元素?

[英]How do I access an element in a list item within a key of a dict?

我目前有一個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文件,我想優化和提取例如“ weather”中的“ main”值,因此我想提取字符串“ Clouds”,但是我嘗試了類似dict [ 'weather] ['main'],但它只會引發錯誤。

尋找一些建議:D

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

另外,我建議不要調用特定的字典dict ,因為它會隱藏內置的dict函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM