将json文件保存到计算机python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17518937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving a json file to computer python
提问by ark
Using requests
in Python I am performing a GET, requesting a JSON file which I can later access and modify, tweak, etc. with the command solditems.json()
. However I would like to save this JSON file to my computer. Looking through the requests docs I found nothing, does anybody have an easy way I can do this?
requests
在 Python 中使用我正在执行 GET,请求一个 JSON 文件,我以后可以使用命令访问和修改、调整等solditems.json()
。但是我想将此 JSON 文件保存到我的计算机。查看请求文档我一无所获,有没有人有一种简单的方法可以做到这一点?
采纳答案by Jared
You can do it just as you would without requests
. Your code might look something like,
您可以像没有requests
. 你的代码可能看起来像,
import json
import requests
solditems = requests.get('https://github.com/timeline.json') # (your url)
data = solditems.json()
with open('data.json', 'w') as f:
json.dump(data, f)
回答by hzitoun
Based on @Lukasa's comment, this accelerates @Jared's solution :
根据@Lukasa 的评论,这加速了@Jared 的解决方案:
import requests
solditems = requests.get('https://github.com/timeline.json') # (your url)
data = solditems.content
with open('data.json', 'wb') as f:
f.write(data)