6

I'm new in python and I use Google Colab . I uploaded a train_data.npy into google Colab and then I want to use it . According to this link How to import and read a shelve or Numpy file in Google Colaboratory?

when i run my code i face this error :

TypeError: 'dict_keys' object does not support indexing

Here is my code :

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

hdiz
  • 1,141
  • 2
  • 13
  • 27

2 Answers2

5

Here's an adjustment to your snippet that will save any uploaded file in the current directory using the uploaded file name.

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)
Bob Smith
  • 36,107
  • 11
  • 98
  • 91
-1

First upload your local data:

from google.colab import files

uploaded = files.upload()

Access it using:

import io

df = pd.read_csv(io.BytesIO(uploaded['Filename.csv']))
Rayan Ral
  • 1,862
  • 2
  • 17
  • 17