【问题标题】:API - TypeError: list indices must be integers, not strAPI - TypeError:列表索引必须是整数,而不是 str
【发布时间】:2017-11-24 05:27:28
【问题描述】:

存储 API 数据的所有变量:

formatted_data = json_data['list'][0]['main'] # Extracting the main description from the API
formatted_data3 = json_data['list'][0]['weather']['description'] # Extracting the weather description from the API
formatted_data2 = json_data['list'][0]['main']['temp'] # Extracting the temperature description from the API
formatted_data4 = json_data['list'][0]['main']['temp_min']
formatted_data5 = json_data['list'][0]['main']['temp_max']
formatted_data6 = json_data['list'][0]['clouds']['all']

谁能帮我解决这个错误?

Traceback (most recent call last):
File "MainWeatherAPI.py", line 21, in <module>
print(ForecastAPIFunction.getForecast(city, country))
File "/home/codio/workspace/Weatherbot/ForecastAPIFunction.py", line 
13, in getForecast
formatted_data3 = json_data['list'][0]['weather']['description'] 
#Extracting the weather description from the API

编辑:

与我目前尝试过的代码相关的代码:

while True:
    address = input("Address: ")
    if address == 'quit' or address == 'q':
        break
    url = main_api + urllib.parse.urlencode({'address': address}) 
    json_data = requests.get(url).json() 
    #print(url)
    json_status = json_data['status'] print('API Status: ' + json_status + '\n')
    if json_status == 'OK':
        for each in json_data['results'][0]['address_components']:
            print(each['long_name'])

编辑:

与我用来存储数据的所有变量相关的示例 API 数据。这也与我收到的错误有关。

{
  'cnt': 40, 
  'city': {
    'name': 'Coventry', 
    'country': 'GB', 
    'id': 2652221, 
    'coord': {
      'lat': 52.4066, 
      'lon': -1.5122}
  }, 
  'message': 0.165, 
  'list': [
    {'sys': {'pod': 'n'}, 
    'dt_txt': '2017-11-23 21:00:00', 
    'weather': [
      {
        'main': 'Clouds', 
        'icon': '04n', 
        'id': 803, 
        'description': 'broken clouds'
      }
    ], 
    'rain': {}, 
    'main': {
      'temp_max': 278.37,
      'grnd_level': 1005.61, 
      'temp': 278.37, 
      'sea_level': 1018.48,
      'humidity': 79, 
      'temp_kf': 0.15, 
      'pressure': 1005.61, 
      'temp_min': 278.226}, 
      'clouds': {'all': 56}, 
      'dt': 1511470800, 
      'wind': {'deg':''}
}

【问题讨论】:

  • 请使用 JSON 数据样例编辑您的帖子。
  • while True: address = input("Address: ") if address == 'quit' 或 address == 'q': break url = main_api + urllib.parse.urlencode({'address' : 地址}) json_data = requests.get(url).json() #print(url) json_status = json_data['status'] print('API 状态: ' + json_status + '\n') if json_status == 'OK ': for each in json_data['results'][0]['address_components']: print(each['long_name'])
  • [:0] 更改为[0]
  • 不幸遇到同样的错误
  • @aaromated 正如我在其他评论中提到的那样。请使用来自 API 的 JSON 数据的示例输出来编辑您的帖子。调试问题会更容易。

标签: python


【解决方案1】:

试试这个

formatted_data3 = json_data['list'][0]['weather'][0]['description'] 

天气元素也是数组

【讨论】:

  • return "描述:" + formatted_data + ", " + formatted_data3 + "\n" + "云:" + str(formatted_data6) + ("%") + ("\n") + (“温度:”)+ str(celsius_temp)+“°C”+“\n”+“平均值:”+ str(celsius_min)+“到”+ str(celsius_max)+“°C”类型错误:不能将'dict'对象隐式转换为str,会导致这个错误
  • formatted_data 是一个字典,因此请尝试使用例如 formatted_data['temp'] 或每个字典元素以类似的方式分别连接到您的输出字符串...
  • 类似这样的东西,也许是:return "Description:" + formatted_data3 + "\n" + "Clouds:" + str(formatted_data6) + ("%") + ("\n") + ("温度:") + str(celsius_temp) + "°C" + "\n" + "平均值:" + str(celsius_min) + " to " + str(celsius_max) + "°C"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多