python中npz文件的读取,如何更改.npz文件中的值?

I want to change one value in an npz file.

The npz file contains several npy's, I want all but one ( 'run_param' ) to remain unchanged and I want to save over the original file.

This is my working code:

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']

This produces:

0

0

0

1

I can't seem to change the value in the original npy.

My code to save afterward is:

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

ass_file.close()

How to make this work?

解决方案

Why your code did not work

What you get from np.load is a NpzFile, which may look like a dictionary but isn't. Every time you access one if its items, it reads the array from file, and returns a new object. To demonstrate:

>>> 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

The id function for the same object is always the same. From the Python 3 Manual:

id(object)

Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. ...

This means that in our case, each time we call npz['test_data'] we get a new object. This "lazy reading" is done to preserve memory and to read only the required arrays. In your code, you modified this object, but then discarded it and read a new one later.

So what can we do?

If the npzfile is this weird NpzFile instead of a dictionary, we can simply convert it to a dictionary:

>>> 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.]])}

You can edit the dictionary at will and save it.

### 回答1: 可以使用 `numpy.load()` 函数读取 `.npz` 文件,该函数返回一个字典对象,其包含了保存在文件的所有数组。 示例代码如下: ```python import numpy as np # 读取 npz 文件 data = np.load('file.npz') # 获取字典的数组 arr1 = data['arr_0'] arr2 = data['arr_1'] # 使用数组 print(arr1) print(arr2) ``` 其,`arr_0` 和 `arr_1` 是保存在 `.npz` 文件的数组的键。如果文件只保存了一个数组,则可以使用 `data['arr_0']` 直接获取该数组。 ### 回答2: Python可以使用NumPy库来读取和操作.npz文件。以下是Python读取.npz文件的步骤: 首先,确保已经安装了NumPy库。如果没有安装,可以使用以下命令进行安装:pip install numpy。 然后,导入NumPy库:import numpy as np。 接下来,可以使用np.load()函数来加载.npz文件。该函数接受文件路径作为参数,并返回一个包含所有数据的字典对象。 例如,假设有一个名为data.npz文件,其包含了名为arr1和arr2的两个NumPy数组。可以使用以下代码将.npz文件加载到Python程序: data = np.load('data.npz') 加载后的数据存储在字典对象data,可以通过下标来访问其的数组。例如,要访问arr1数组,可以使用data['arr1']。 例如,要打印arr1数组的内容,可以使用以下代码: print(data['arr1']) 可以通过调用np.savez()函数来保存多个数组为一个.npz文件。该函数接受文件路径和需要保存的数据作为参数,可以指定参数名。 例如,将两个数组arr1和arr2保存到一个名为data.npz文件,可以使用以下代码: np.savez('data.npz', arr1=array1, arr2=array2) 这样就完成了从.npz文件读取数据的过程。通过了解NumPy库的其他函数和方法,可以根据需要对读取的数据进行进一步的处理和操作。 ### 回答3: npz文件是一种存储多个numpy数组的文件格式。要读取npz文件,可以使用numpy库的load函数。 具体步骤如下: 1. 首先,需要确保安装了numpy库。可以使用pip install numpy命令来安装numpy。 2. 然后,使用load函数来读取npz文件。load函数的参数是npz文件的路径。例如,如果npz文件位于当前目录下的data.npz文件,可以使用load("data.npz")来读取文件。 3. load函数返回一个类似于字典的对象,其包含了npz文件的所有数组。可以通过指定键来访问特定的数组。例如,如果npz文件包含了名为array1和array2的两个数组,可以使用loaded_data["array1"]来访问array1数组。 4. 最后,可以对读取到的数组进行各种操作,例如打印、计算等。 总结: 要读取npz文件,可以使用numpy库的load函数。load函数返回一个类似于字典的对象,其包含了npz文件的所有数组。通过指定键可以访问特定的数组。读取后可以对数组进行各种操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值