【发布时间】:2018-11-21 09:28:01
【问题描述】:
我有一个二进制文件,其中包含一些编码为 BCD(二进制编码十进制)的字段。示例如下。
14 75 26 58 87 7F(十六进制格式的原始字节)。
我正在使用 (np.void, 6) 从二进制文件读取和转换,下面是我得到的输出。
b'\x14\x75\x26\x58\x87\x7F'
但我想使用 numpy 将输出作为“14752658877”,不使用填充字符“F”。
下面是代码: 用 open (filename, "rb") as f:
while True:
chunk = f.read(chunksize)
if (chunk):
dt = np.dtype([('a','b'), ('b', '>i4'), ('c', 'S15'),('d', np.str, 7),
('e', 'S7'), ('f', np.void, 6)])
x = np.frombuffer (chunk, dtype=dt)
print (x)
else:
break
此外,输入文件包含许多固定长度的二进制记录。使用 numpy 将其转换并存储为 ascii 文件的有效方法是什么?
【问题讨论】:
-
显示示例代码。
-
下面是代码:with open (filename, "rb") as f: while True: chunk = f.read(chunksize) if (chunk): dt = np.dtype([(' a','b'), ('b', '>i4'), ('c', 'S15'),('d', np.str, 7), ('e', 'S7') , ('f', np.void, 6)]) x = np.frombuffer (chunk, dtype=dt) print (x) else: break
-
请编辑您的原始问题,添加上述代码,保留所有格式和缩进。
-
嗨,Ricardo,我已经编辑了原始问题以添加代码。
-
F 不是填充字符。它是十六进制值的一部分。