python中npz文件的读取,使用numpy.load从文件加载压缩数据(.npz)

这篇博客讨论了如何在Python中使用numpy.load函数读取和访问.npz压缩文件中的数据。作者通过示例代码展示了保存和加载数据的过程,并遇到了在IDLE环境中运行时的错误,但该问题在IPython环境中得到了解决。解决方案是直接传入.npz文件名,numpy将自动解压缩并允许访问数组数据。
摘要由CSDN通过智能技术生成

I have an array:

>>> data = np.ones((1,3,128))

I save it to file using savez_compressed:

>>> with open('afile','w') as f:

np.savez_compressed(f,data=data)

When I try to load it I don't seem to be able to access the data:

>>> with open('afile','r') as f:

b=np.load(f)

>>> b.files

['data']

>>> b['data']

Traceback (most recent call last):

File "", line 1, in

b['data']

File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 238, in __getitem__

bytes = self.zip.read(key)

File "C:\Python27\lib\zipfile.py", line 828, in read

return self.open(name, "r", pwd).read()

File "C:\Python27\lib\zipfile.py", line 853, in open

zef_file.seek(zinfo.header_offset, 0)

ValueError: I/O operation on closed file

Am I doing something obviously wrong?

EDIT

Following @Saullo Castro's answer I tried this:

>>> np.savez_compressed('afile.npz',data=data)

>>> b=np.load('afile.npz')

>>> b.files

['data']

>>> b['data']

and got the following error:

<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值