【问题标题】:Parsing data in dictionary (json) using openweathermap api使用openweathermap api解析字典(json)中的数据
【发布时间】:2018-04-10 01:40:03
【问题描述】:

在探索、学习的过程中,我对 Python 还是很陌生,而今天我正在使用 JSON。每次出现在我的字典中时,如何解析和打印“dt_txt”键和相应的值?这是我的代码。

import requests, pytemperature, json

r = requests.get('http://samples.openweathermap.org/data/2.5/forecast?
lat=35&lon=139&appid=b1b15e88fa797225412429c1c50c122a1')
dict = r.json()

for key, value in dict.items():
    if 'dt_txt' in str(key):
        print(key)

这是 JSON 快照或上面链接中的完整内容。

    {
"cod": "200",
"message": 0.179,
"cnt": 40,
"list": [{
        "dt": 1509202800,
        "main": {
            "temp": 297.18,
            "temp_min": 291.573,
            "temp_max": 297.18,
            "pressure": 1027.02,
            "sea_level": 1029.75,
            "grnd_level": 1027.02,
            "humidity": 68,
            "temp_kf": 5.6
        },
        "weather": [{
                "id": 500,
                "main": "Rain",
                "description": "light rain",
                "icon": "10d"
            }
        ],
        "clouds": {
            "all": 88
        },
        "wind": {
            "speed": 1.61,
            "deg": 99.0033
        },
        "rain": {
            "3h": 0.09
        },
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2017-10-28 15:00:00"
    }, {
        "dt": 1509213600,
        "main": {
            "temp": 297.32,
            "temp_min": 293.116,
            "temp_max": 297.32,
            "pressure": 1024.56,
            "sea_level": 1027.16,
            "grnd_level": 1024.56,
            "humidity": 76,
            "temp_kf": 4.2
        },
        "weather": [{
                "id": 500,
                "main": "Rain",
                "description": "light rain",
                "icon": "10d"
            }
        ],
        "clouds": {
            "all": 48
        },
        "wind": {
            "speed": 1.96,
            "deg": 173.002
        },
        "rain": {
            "3h": 0.41
        },
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2017-10-28 18:00:00"

旁注:最后我尝试打印日期、temp_min、temp_max、main 和描述。我会将温度从开尔文转换为华氏温度,然后每天使用 gmail 向我发送新预测的短信。提前感谢您的帮助!

【问题讨论】:

    标签: python json dictionary python-requests openweathermap


    【解决方案1】:

    您可以通过 your_dict['your_value'] 所有每个 dict 键,试试这个:

    import requests, json
    
    r = requests.get('http://samples.openweathermap.org/data/2.5/forecast?lat=35&lon=139&appid=b1b15e88fa797225412429c1c50c122a1')
    dict = r.json()
    select_data = dict['list']
    
    for box in select_data:
    
        if 'dt_txt' in box:
            print(box['dt_txt'])
    
        else:
            print('not found')
    

    【讨论】:

    • 谢谢,这完全回答了我的问题。
    • @MatthewB: 所以请勾选我的答案 :)
    • 我确实勾选了它,但堆栈溢出说我需要至少 15 个声望点才能投票。如果可以的话,我会否决这条规则
    • 现在怎么样? :)
    • 上面写着“感谢您的反馈!声望低于 15 人的投票会被记录下来,但不要更改公开显示的帖子得分。”
    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多