英文:
How to save the file with a json response
问题
响应得到的是一个类似以下内容的JSON:
{'additionalErrors': None,
'data': {'contentType': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'file': 'UEsDBBQACAAIAAAAAAAAAAAAAAAAAAAAAAATAAAAeGwvdGhlbWUvdGhlbWUxLnhtbOyZz2/bNhT...
(还有大约11k个字符)
...hsL3N0eWxlcy54bWxQSwUGAAAAAAsACwDGAgAASB4AAAAA',
'name': 'export-data.xlsx'},
'error': False,
'errorText': ''}
有可能通过这些数据将文件下载到计算机上吗?
我不知道如何解决这个问题。
英文:
response gets a json of something like this:
{'additionalErrors': None,
'data': {'contentType': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'file': 'UEsDBBQACAAIAAAAAAAAAAAAAAAAAAAAAAATAAAAeGwvdGhlbWUvdGhlbWUxLnhtbOyZz2/bNhT...
(there are still about 11k characters)
...hsL3N0eWxlcy54bWxQSwUGAAAAAAsACwDGAgAASB4AAAAA',
'name': 'export-data.xlsx'},
'error': False,
'errorText': ''}
Is it possible, having this data, to download the file to a computer?
I don't know how to solve this problem
答案1
得分: 1
你在收到这个回复时已经下载了文件,它只是以Base64格式进行编码。要解码它,你可以按照以下步骤进行操作:
import base64
resp = ... # 获取你的JSON响应
decoded = base64.b64decode(resp["data"]["file"])
with open("/some/path", "wb") as f:
f.write(decoded)
英文:
You already downloaded the file when you get this response, it's just encoded in base64 format. To decode it, you can do this:
import base64
resp = ... # get your JSON response
decoded = base64.b64decode(resp["data"]["file"])
with open("/some/path", "wb") as f:
f.write(decoded)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论