如何在Google colab中访问Uploaded File

时间:2018-01-28 10:19:09

标签: python google-colaboratory

我是python中的新手,我使用Google Colab。我已将train_data.npy上传到Google Colab,然后我想使用它。根据此链接How to import and read a shelve or Numpy file in Google Colaboratory?

当我运行我的代码时,我面临这个错误:

  

TypeError:'dict_keys'对象不支持索引

这是我的代码:

uploaded = files.upload()

for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name=fn, length=len(uploaded[fn])))

with open('train_data.npy', 'w') as f:
f.write(uploaded[uploaded.keys()[0]])

谢谢

2 个答案:

答案 0 :(得分:5)

以下是对您的代码段的调整,该代码段将使用上传的文件名将任何上传的文件保存在当前目录中。

from google.colab import files
uploaded = files.upload()

for name, data in uploaded.items():
  with open(name, 'wb') as f:
    f.write(data)
    print ('saved file', name)

答案 1 :(得分:-1)

首先上传您的本地数据:

from google.colab import files

uploaded = files.upload()

使用以下方式访问它:

import io

df = pd.read_csv(io.BytesIO(uploaded['Filename.csv']))