Python 如何更改.npz文件中的值?

Python 如何更改.npz文件中的值?,python,numpy,Python,Numpy,我想更改npz文件中的一个值 npz文件包含多个npy,我希望除一个(“run_param”)之外的所有文件保持不变,并且我希望保存在原始文件上 这是我的工作代码: DATA_DIR = 'C:\\Projects\\Test\\data\\' ass_file = np.load( DATA_DIR + 'assumption.npz' ) run_param = ass_file['run_param'] print ass_file['run_param'][0]['RUN_MODE']

我想更改
npz
文件中的一个值

npz
文件包含多个
npy
,我希望除一个(“
run_param
”)之外的所有文件保持不变,并且我希望保存在原始文件上

这是我的工作代码:

DATA_DIR = 'C:\\Projects\\Test\\data\\'
ass_file = np.load( DATA_DIR + 'assumption.npz' )
run_param = ass_file['run_param']

print ass_file['run_param'][0]['RUN_MODE']
ass_file['run_param'][0]['RUN_MODE'] = 1        (has no effect)
print ass_file['run_param'][0]['RUN_MODE']

print run_param[0]['RUN_MODE']
run_param[0]['RUN_MODE'] = 1
print run_param[0]['RUN_MODE']
这将产生:

0
0
0
1
我似乎无法更改原始
npy
中的值

我以后要保存的代码是:

np.savez( DATA_DIR + 'assumption.npz', **ass_file )   #
ass_file.close()

如何进行此操作?

使用
numpy.savez
**kwds,数组将与关键字名称一起保存

   >>> outfile = TemporaryFile()
   >>> np.savez(outfile, x=x, y=y)
   >>> outfile.seek(0)
   >>> npzfile = np.load(outfile)
   >>> npzfile.files
   ['y', 'x']
   >>> npzfile['x']
   array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 >>> np.savez(outfile, x, y)
 >>> outfile.seek(0) # Only needed here to simulate closing & reopening file
 >>> npzfile = np.load(outfile)
 >>> npzfile.files
 ['arr_1', 'arr_0']
 >>> npzfile['arr_0']
 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
当 将
savez
与“just”
*args
一起使用,数组以默认名称保存

   >>> outfile = TemporaryFile()
   >>> np.savez(outfile, x=x, y=y)
   >>> outfile.seek(0)
   >>> npzfile = np.load(outfile)
   >>> npzfile.files
   ['y', 'x']
   >>> npzfile['x']
   array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 >>> np.savez(outfile, x, y)
 >>> outfile.seek(0) # Only needed here to simulate closing & reopening file
 >>> npzfile = np.load(outfile)
 >>> npzfile.files
 ['arr_1', 'arr_0']
 >>> npzfile['arr_0']
 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
至少重新阅读docstring numpy帮助并使用建议的语法

   print numpy.savez.__doc__
为什么你的代码不起作用 你从
np.load
中得到的是a,它看起来像字典,但实际上不是。每次访问其项时,它都从文件中读取数组,并返回一个新对象。证明:

>>> import io
>>> import numpy as np
>>> tfile = io.BytesIO()  # create an in-memory tempfile
>>> np.savez(tfile, test_data=np.eye(3))  # save an array to it
>>> tfile.seek(0)  # to read the file from the start
0
>>> npzfile = np.load(tfile)
>>> npzfile['test_data']
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> id(npzfile['test_data'])
65236224
>>> id(npzfile['test_data'])
65236384
>>> id(npzfile['test_data'])
65236704
同一对象的
id
函数始终相同。从:

id(对象) 返回对象的“标识”。这是一个整数,保证该对象在其生存期内唯一且恒定

这意味着在我们的例子中,每次调用
npz['test_data']
我们都会得到一个新对象。这种“延迟读取”是为了保留内存并只读取所需的数组。在代码中,您修改了此对象,但随后丢弃了它,并在以后读取了一个新对象


那么我们能做什么呢? 如果
npzfile
是一个奇怪的
npzfile
而不是一个字典,我们可以简单地将它转换成一个字典:

>>> mutable_file = dict(npzfile)
>>> mutable_file['test_data'][0,0] = 42
>>> mutable_file
{'test_data': array([[ 42.,   0.,   0.],
                     [  0.,   1.,   0.],
                     [  0.,   0.,   1.]])}
您可以随意编辑词典并保存。

如果您有多个字段 您可能希望保留这是执行此操作的简单工作流

读这个文件来听写 修改条目 将文件写回
“我也不确定这是否有效”-你没有测试过吗?我可能不应该在我的问题中包括关于“保存后”的部分,因为我自己确实没有花足够的时间尝试它。我的主要问题是更改“ass_file”中的值@容貌
np.savez(filename, **filedic)