【发布时间】:2016-11-12 00:32:27
【问题描述】:
我正在尝试获取此 api 请求并在将其转储到 JSON 文件时对其进行格式化。每当我这样做时,它都是一个字符串并且很难阅读。我试过添加缩进,但它没有做任何事情。如果需要,我可以提供 API 密钥。
import json, requests
url = "http://api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID={APIKEY}"
response = requests.get(url)
response.raise_for_status()
with open('weather.json', 'w') as outfile:
json.dump(response.text, outfile, indent=4)
【问题讨论】:
-
indent 参数应该可以工作。您是否从以前的尝试中查看过陈旧的文件,新的尝试失败并且不再接触文件?
标签: python json api formatting request