簡體   English   中英

在python 3中解壓縮二進制文件

[英]unpack binary file in 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())

我收到錯誤消息“ struct.error:結構格式錯誤的字符”

謝謝!

那么使用struct呢?

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)

它會在某個地方破裂,你會知道在哪里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM