我是python新手,我使用Google Colab。我上传了一个train_data.npy到谷歌CLAB,然后我想用它。根据此链接How to import and read a shelve or Numpy file in Google Colaboratory?
当我运行代码时,我将面临以下错误:
Type Error:“DigiKiKEY”对象不支持索引
这是我的代码:

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]])

谢谢

最佳答案

下面是对代码片段的调整,它将使用上载的文件名在当前目录中保存任何上载的文件。

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)

10-04 16:28