Numpy.save list of different sized arrays gives: TypeError: '_io.TextIOWrapper' object is not subscriptable
Numpy.save list of different sized arrays gives: TypeError: '_io.TextIOWrapper' object is not subscriptable
这是我的代码:
import numpy as np
mylist = [np.arange(0,1, 0.5), np.arange(0,2,0.5)]
np.save('mylist', mylist)
with open('mylist.npy') as last:
print(lst[0])
我遇到了错误
TypeError Traceback (most recent call last)
<ipython-input-7-2461c8fce207> in <module>
6
7 with open('mylist.npy') as lst:
---> 8 print(lst[0])
TypeError: '_io.TextIOWrapper' object is not subscriptable
我已经尝试根据其他类似的帖子修改代码,例如包含 lst = lst.read()
或 lst = lst.readlines()
,但这给了我一个 Unicode 错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x93 in position 0: invalid start byte
有人有什么建议吗?谢谢!
正如 hpaulj 评论的那样,解决方案是使用 np.load
而不是 read
或 readlines
。
import numpy as np
mylist = [np.arange(0,1, 0.5), np.arange(0,2,0.5)]
np.save('mylist', mylist)
with open('mylist.npy') as last:
lst = np.load(lst, allow_pickle = True)
print(lst[0])
这是我的代码:
import numpy as np
mylist = [np.arange(0,1, 0.5), np.arange(0,2,0.5)]
np.save('mylist', mylist)
with open('mylist.npy') as last:
print(lst[0])
我遇到了错误
TypeError Traceback (most recent call last)
<ipython-input-7-2461c8fce207> in <module>
6
7 with open('mylist.npy') as lst:
---> 8 print(lst[0])
TypeError: '_io.TextIOWrapper' object is not subscriptable
我已经尝试根据其他类似的帖子修改代码,例如包含 lst = lst.read()
或 lst = lst.readlines()
,但这给了我一个 Unicode 错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x93 in position 0: invalid start byte
有人有什么建议吗?谢谢!
正如 hpaulj 评论的那样,解决方案是使用 np.load
而不是 read
或 readlines
。
import numpy as np
mylist = [np.arange(0,1, 0.5), np.arange(0,2,0.5)]
np.save('mylist', mylist)
with open('mylist.npy') as last:
lst = np.load(lst, allow_pickle = True)
print(lst[0])