TypeError: string indices must be integers JSON File
TypeError: string indices must be integers JSON File
不过,我对这一切还很陌生。在我的代码中,我试图让 Python 从一些免费天气 API 中获取天气信息。然而,尽管一切都会按计划进行,它会得到天气 - 晴朗但是当涉及到整数时它会吓坏了,并且所需的大部分信息都是整数:温度,湿度,风速等。
这是它失败的地方:
for result in data['main']:
temp = result['temp']
print('Temperature: '+temp)
with open('data.txt', 'a') as f:
f.write(' '+temp)
f.write('\n')
Table 看起来像:
'main': {'feels_like': 304.72,
'humidity': 67,
'pressure': 1011,
'temp': 301.84,
'temp_max': 303.85,
'temp_min': 299.19},
主要是词典。
我尝试添加 [3]
而不是 ['temp'\]
for result in data['main']:
temp = result[3]
print('Temperature: '+temp)
with open('data.txt', 'a') as f:
f.write(' '+temp)
f.write('\n')
但是,输出是每个类别的前 3 个字母,例如 TEM
- TEMP / HUM
- HUMIDITY
我知道它做了什么,但我只是信任某个网站。我不知道有什么方法可以解决这个问题,很多网站只是说“更改 JSON 文件”,但我真的做不到。
我试过 'Json.Loads' 但它说,必须 'str, bytes, or bytearray, not dict.'
回溯
File "location", line 30, in <module>
temp = result['temp']
TypeError: string indices must be integers
您的代码:
for result in data['main']:
temp = result[3]
print('Temperature: '+temp)
with open('data.txt', 'a') as f:
f.write(' '+temp)
f.write('\n')
遍历数据['main']的键值。也就是说,每一次循环,result
都是["feels_like", "humidity", "pressure", "temp", "temp_max", "temp_min"]
列表中的一个值
你真正想做的事情:
for result in data["main"]:
if result == "temp":
print("Temperature: %0.2f" % data["main"][result])
with open("data.txt", "a") as f:
f.write(" %0.2f\n" % data["main"][result])
甚至:
for field, result in data["main"].items():
if field == "temp":
print("Temperature: %0.2f" % result)
with open("data.txt", "a") as f:
f.write(" %0.2f\n" % result)
从字典 data["main"]
.
中获取键和值
你可以这样做:
for key, result in data['main'].items():
if key == 'temp':
print(f'Temperature: result')
#rest of your code
不过,我对这一切还很陌生。在我的代码中,我试图让 Python 从一些免费天气 API 中获取天气信息。然而,尽管一切都会按计划进行,它会得到天气 - 晴朗但是当涉及到整数时它会吓坏了,并且所需的大部分信息都是整数:温度,湿度,风速等。 这是它失败的地方:
for result in data['main']:
temp = result['temp']
print('Temperature: '+temp)
with open('data.txt', 'a') as f:
f.write(' '+temp)
f.write('\n')
Table 看起来像:
'main': {'feels_like': 304.72,
'humidity': 67,
'pressure': 1011,
'temp': 301.84,
'temp_max': 303.85,
'temp_min': 299.19},
主要是词典。
我尝试添加 [3]
而不是 ['temp'\]
for result in data['main']:
temp = result[3]
print('Temperature: '+temp)
with open('data.txt', 'a') as f:
f.write(' '+temp)
f.write('\n')
但是,输出是每个类别的前 3 个字母,例如 TEM
- TEMP / HUM
- HUMIDITY
我知道它做了什么,但我只是信任某个网站。我不知道有什么方法可以解决这个问题,很多网站只是说“更改 JSON 文件”,但我真的做不到。
我试过 'Json.Loads' 但它说,必须 'str, bytes, or bytearray, not dict.'
回溯
File "location", line 30, in <module>
temp = result['temp']
TypeError: string indices must be integers
您的代码:
for result in data['main']:
temp = result[3]
print('Temperature: '+temp)
with open('data.txt', 'a') as f:
f.write(' '+temp)
f.write('\n')
遍历数据['main']的键值。也就是说,每一次循环,result
都是["feels_like", "humidity", "pressure", "temp", "temp_max", "temp_min"]
你真正想做的事情:
for result in data["main"]:
if result == "temp":
print("Temperature: %0.2f" % data["main"][result])
with open("data.txt", "a") as f:
f.write(" %0.2f\n" % data["main"][result])
甚至:
for field, result in data["main"].items():
if field == "temp":
print("Temperature: %0.2f" % result)
with open("data.txt", "a") as f:
f.write(" %0.2f\n" % result)
从字典 data["main"]
.
你可以这样做:
for key, result in data['main'].items():
if key == 'temp':
print(f'Temperature: result')
#rest of your code