【问题标题】:"NameError: name api_key is not defined" when parsing json解析 json 时出现“NameError: name api_key is not defined”
【发布时间】:2018-09-05 03:29:24
【问题描述】:

我正在完成一项任务,但遇到了一些麻烦。 (对此非常陌生)我收到“NameError: name api_key is not defined”消息。该单元格中的语法如下:

record = 0
for index, row in location_data.iterrows():
    city_name = row['city']
    country_code = row['country']
    url = target_url + city_name + ',' + country_code + '&units=' + units + '&APPID' + api_key
    print (url)
    try: 
        weather_response = req.get(url)
        weather_json = weather_response.json()
        latitude = weather_json["coord"]["lat"]
        longitude = weather_json["coord"]["lon"]
        temp = weather_json["main"]["temp"]
        humidity = weather_json["main"]["humidity"]
        cloud = weather_json["clouds"]["all"]
        wind = weather_json["wind"]["speed"]
        location_data.set_value(index,"Temp", temp)
        location_data.set_value(index,"Humidity",humidity)
        location_data.set_value(index,"Wind Speed", wind)
        location_data.set_value(index,"Cloudiness",cloud)
        location_data.set_value(index,"Lat", latitude)
        location_data.set_value(index,"Longitude",longitude)
        print("Retrieved data for %s, %s" % (city_name, country_code))
    except:
        print("No data for %s, %s" % (city_name,country_code))
    record += 1
    if record % 59 == 0:
        time.sleep(60)

感谢您的任何帮助。

【问题讨论】:

    标签: json syntax api-key


    【解决方案1】:

    url = ... 行中,您正在使用一个名为api_key 的变量,该变量未定义。

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2015-11-11
      • 2012-05-22
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 2023-02-05
      • 1970-01-01
      • 2020-11-07
      相关资源
      最近更新 更多