我是 python 新手,我使用Google Colab
。我上传了一个train_data.npy
进入google Colab,然后我想使用它。根据这个链接如何在 Google Colaboratory 中导入并读取 shelve 或 Numpy 文件? https://stackoverflow.com/questions/47212852/how-to-import-and-read-a-shelve-or-numpy-file-in-google-colaboratory
当我运行我的代码时,我遇到这个错误:
类型错误:“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]])
Thanks
这是对您的代码片段的调整,它将使用上传的文件名将所有上传的文件保存在当前目录中。
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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)