如何在Google Colaboratory中导入和读取shelve或Numpy文件?

Sae*_*eed 11 python google-notebook jupyter-notebook google-colaboratory

我有file.npy,我想在Google Colaboratory Notebook中加载它.我已经知道我必须从Google云端硬盘加载该文件,但我不知道该怎么做.

欢迎任何帮助

小智 17

使用以下内容将您的文件上传到Colaboratory Notebook:

from google.colab import files
uploaded = files.upload()
Run Code Online (Sandbox Code Playgroud)

然后,您可以从uploaded对象访问文件的内容,然后将其写入文件:

with open("my_data.h5", 'w') as f:
    f.write(uploaded[uploaded.keys()[0]])
Run Code Online (Sandbox Code Playgroud)

如果您运行:

!ls
Run Code Online (Sandbox Code Playgroud)

你会my_data.h5在当前目录中看到文件.

这是对我有用的方法.希望能帮助到你.


Bob*_*ith 7

实际上,您可以直接上传和下载本地文件.

I/O示例笔记本中有本地文件上载/下载以及驱动器文件上载/下载的示例

第一个单元格显示本地文件上传:

from google.colab import files
uploaded = files.upload()
for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name=fn, length=len(uploaded[fn])))
Run Code Online (Sandbox Code Playgroud)