如何使用python在Google Colab中保存JSON转储?

时间:2019-07-11 06:08:57

标签: python json google-colaboratory

x = json.dumps(....)

如何将x保存到json文件并将其存储在Google驱动器中,或使用Google colab将其下载到本地PC

我在本地PC上当前正在使用此

 with open('data.json', 'w') as f:
    f.write(x.encode('utf-8'))

2 个答案:

答案 0 :(得分:1)

将其下载到本地驱动器reference here

from google.colab import files
import json

x = json.dumps ('test')
y = x.encode('utf-8')
with open('example.txt', 'w') as f:
  f.write(str(y))

files.download('example.txt')

或者,正如我之前在评论中提到的那样,您可以使用提供的脚本来安装Google驱动器,或者直接单击安装驱动器,然后运行提供的单元。挂载之后,您现在可以像本地驱动器fyr一样对其进行写入。

Mount Google Drive

答案 1 :(得分:0)

如克里斯托弗(Christopher)所建议,您可以帮助this。该链接将帮助您最终将Google Colab挂载为本地驱动器。完成此操作后,您可以轻松地与Google Colab进行文件读写。 我认为,值得尝试,并且应该可以工作。 注意:我尚未尝试和测试此解决方案。