我必须读取和写入二进制数据,其中每个数据元素都是:
是否可以不使用任何外部模块?如果是的话,
答案 0 :(得分:10)
我认为您最好使用array
模块。它默认以系统字节顺序存储数据,但您可以使用array.byteswap()
在字节顺序之间进行转换,并且可以使用sys.byteorder
来查询系统字节顺序。例如:
# Create an array of 16-bit signed integers
a = array.array("h", range(10))
# Write to file in big endian order
if sys.byteorder == "little":
a.byteswap()
with open("data", "wb") as f:
a.tofile(f)
# Read from file again
b = array.array("h")
with open("data", "rb") as f:
b.fromfile(f, 10)
if sys.byteorder == "little":
b.byteswap()
答案 1 :(得分:2)
from array import array
# Edit:
from sys import byteorder as system_endian # thanks, Sven!
# Sigh...
from os import stat
def read_file(filename, endian):
count = stat(filename).st_size / 2
with file(filename, 'rb') as f:
result = array('h')
result.fromfile(f, count)
if endian != system_endian: result.byteswap()
return result
答案 2 :(得分:1)
考虑使用
struct.unpack(byteorder + str(len(rawbytes) // 2) + "h", rawbytes)
其中byteorder
根据需要为'<'
或'>'
,同样适用于打包。注意:我并未声称这比array
方式更快,但我注意到array
方式有时需要额外的byteswap
步骤。
答案 3 :(得分:0)
我发现这对于从二进制文件读取/写入数据到numpy数组非常有用:
import numpy as np
sys.argv[1] = endian # Pass endian as an argument to the program
if endian == 'big':
precTypecode = '>'
elif endian == 'little':
precTypecode = '<'
# Below: 'i' is for signed integer and '2' is for size of bytes.
# Alternatively you can make this an if else statement to choose precision
precTypecode += 'i2'
im = np.fromfile(inputFilename, dtype = precTypecode) # im is now a numpy array
# Perform any operations you desire on 'im', for example switching byteorder
im.byteswap(True)
# Then write to binary file (note: there are some limitations, so refer doc)
im.tofile(outputFilename)
希望这有帮助。
答案 4 :(得分:0)
如所提出的,没有任何外部模块:
with open("path/file.bin", "rb") as file:
byte_content = file.read()
list_16bits = [byte_content[i + 1] << 8 | byte_content[i] for i in range(0, len(byte_content), 2)]
在理解列表中,我们读取每两个字节。然后,通过按位运算,我们连接这2个字节。这取决于在哪里写i+1
和i