python - 如何使用python将NPZ文件转换为文本文件

使用这个简单的代码将 NPZ 文件转换为文本文件。

import numpy as np
import sys
data = np.load("file_name.npz")
print(data.files)
row = data.files
np.set_printoptions(threshold=np.inf)
print(data['arr_0'])
sys.stdout=open("test.txt","w")
for i in row:
    print("--------------------------")
    print(data[i])
sys.stdout.close()

最佳答案

#import packages we need
import numpy as np

创建示例 NPZ 文件

myarray = np.array([0,1,2,3])
np.savez('npzfile.npz',array0=myarray) # array0 will be the name with which you can retrieve myarray

读入

data = np.load('npzfile.npz')
data['array0'] # use array0 key to retrieve myarray

结果将是:

array([0, 1, 2, 3])

这是一个 numpy 数组:

type(data['array0'])

正在输出 numpy.ndarray


如果你不知道NPZ是怎么得救的

你可以做 data.files。如果您使用上面的示例 NPZ,这将输出 ['array0']。这可以帮助您在使用 data[name_of_array](即上面的 data[array0])时了解数组的名称。

https://stackoverflow.com/questions/63207159/

相关文章:

python - 多交易所、多币种、多金额的套利算法

python - 使用 pydbgen 模块时出现 TypeError

python - 无法将从 MongoDB 检索到的 json 值返回到 python flask

flutter - Firestore 复合查询不适用于 Flutter Web

python - Python的Tkinter模块如何让对话框窗口出现在最前面?

java - 在 Java 中使用 OpenPDF 居中/对齐表格单元格中的文本

macos - 没有应用程序包的 QT 命令行可执行二进制文件有 Macdeployqt 吗?

python - 当我的播放器类与我的平台类之一发生碰撞时,如何制作和结束屏幕?

python - 如何在不返回并更改数据的情况下更改 seaborn fiddle 图的 x 轴标签

mongodb - 更新嵌套数组的字段时,如何使 mongodb 的 "updated"停止?