Python:使用变量/动态名称保存 JSON 响应?
我对 Python 很陌生,我想知道如何在循环中保存 JSON 响应并根据 API 请求更改命名?
TestList = ["bitcoin", "avalanche", "ethereum"]
TestListLen = len(TestList)
for i in TestList:
# Request JSON response
r = requests.get (f"https://api.coingecko.com/api/v3/coins/{i}/market_chart?vs_currency=usd&days=max&interval=daily")
if r.status_code >= 201:
continue
data = r.json()
# How to save that response as eg. bitcoin.json or ethereum.json according to the names in TestList?
i am pretty new to Python and I am wondering how I can save a JSON response in a loop and change naming according to API request?
TestList = ["bitcoin", "avalanche", "ethereum"]
TestListLen = len(TestList)
for i in TestList:
# Request JSON response
r = requests.get (f"https://api.coingecko.com/api/v3/coins/{i}/market_chart?vs_currency=usd&days=max&interval=daily")
if r.status_code >= 201:
continue
data = r.json()
# How to save that response as eg. bitcoin.json or ethereum.json according to the names in TestList?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需打开一个名为
{i}.json
的文件并将 json 结果转储到其中:Just open a file with name
{i}.json
and dump the json result in it: