在 Python 3 中解压缩二进制文件



你能帮忙解压缩Python 3中的二进制文件吗?它是一个图像 2580*7839 大小,4 字节浮点数。我在 Python 2 的代码中拥有的东西,它有效,但在 Python 3 中不起作用。

bformat= ">%sf"
ns = 2580*7839*4
#open file f
byte_arr=f.read(ns)
unpacked_bytes = unpack(bformat % (ns/4), byte_arr)
data=np.array(unpacked_bytes).reshape(7839,2580)
print ('min value', data.min())
print ('max value', data.max())

我收到错误消息"结构错误:结构格式的错误字符"

谢谢!

使用结构怎么样?

import struct
f0 = struct.unpack('>f', f.read(4))[0]
f1 = struct.unpack('>f', f.read(4))[0]
f2 = struct.unpack('>f', f.read(4))[0]
....

更好的循环

for i in range(0, 2580*7839):
    ff = struct.unpack('>f', f.read(4))[0]
    print(i,ff)

它会在某个地方破裂,你会知道在哪里

最新更新