Numpy savez将我的密钥解释为文件名-> IOError [英] Numpy savez interprets my keys as filenames -> IOError
本文介绍了Numpy savez将我的密钥解释为文件名-> IOError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将 numpy savez 用作推荐以保存numpy数组.作为键,我使用从中加载数据的文件的名称.但是似乎savez
试图以某种方式使用文件名.我该怎么办?我想避免剥离其路径和结尾的文件名.
I am using numpy savez as recommended to save numpy arrays. As keys I use the names of the files I have loaded the data from. But it seems like savez
is trying to use the filenames somehow. What should I do? I would like to avoid stripping the file names of their path and ending.
>>> import numpy
>>> arrs = {'data/a.text': numpy.array([1,2]),
'data/b.text': numpy.array([3,4]),
'data/c.text': numpy.array([5,6])}
>>> numpy.savez('file.npz', **arrs)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.6/dist-packages/numpy/lib/io.py", line 305, in savez
fid = open(filename,'wb')
IOError: [Errno 2] No such file or directory: '/tmp/data/c.text.npy'
推荐答案
您可以在将密钥传递给savez
函数之前对其进行编码和解码.
You can encode and decode the keys before you pass it to the savez
function.
>>> import numpy
>>> import base64
>>> arrs = {'data/a.text': numpy.array([1,2]),
'data/b.text': numpy.array([3,4]),
'data/c.text': numpy.array([5,6])}
>>> numpy.savez('file.npz', **dict((base64.urlsafe_b64encode(k), v)
for k,v in arrs.iteritems()))
>>> npzfile = numpy.load('file.npz')
>>> decoded = dict((base64.urlsafe_b64decode(k), v)
for k,v in npzfile.iteritems())
>>> decoded
{'data/c.text': array([5, 6]),
'data/a.text': array([1, 2]),
'data/b.text': array([3, 4])}
这篇关于Numpy savez将我的密钥解释为文件名-> IOError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文