将 Little-endian 24 位文件转换为 ASCII 数组
Convert Little-endian 24 bits file to an ASCII array
我有一个包含 52 行 html header 的 .raw 文件,后跟数据本身。该文件以 little-endian 24bits SIGNED 编码,我想将数据转换为 ASCII 文件中的整数。我用 Python 3.
我尝试 'unpack' 整个文件,并在 this post 中找到以下代码:
import sys
import chunk
import struct
f1 = open('/Users/anais/Documents/CR_lab/Lab_files/labtest.raw', mode = 'rb')
data = struct.unpack('<i', chunk + ('[=11=]' if chunk[2] < 128 else '\xff'))
但我收到此错误消息:
TypeError: 'module' object is not subscriptable
编辑
好像这样更好:
data = struct.unpack('<i','[=13=]'+ bytes)[0] >> 8
但我仍然收到错误消息:
TypeError: must be str, not type
我想很容易修复?
在 Python 中处理这不是一个很好的文件! Python 非常适合处理文本文件,因为它会在内部缓冲区中以大块的形式读取它们,然后逐行迭代,但是您无法轻松访问像这样读取文本后出现的二进制数据。此外,struct
模块不支持 24 位值。
我能想到的唯一方法是一次一个字节地读取文件,首先跳过 52 次行尾,然后一次读取 3 个字节,将它们连接成一个 4 字节的字节字符串并解压.
可能的代码可能是:
eol = b'\n' # or whatever is the end of line in your file
nlines = 52 # number of lines to skip
with open('/Users/anais/Documents/CR_lab/Lab_files/labtest.raw', mode = 'rb') as f1:
for i in range(nlines): # process nlines lines
t = b'' # to store the content of each line
while True:
x = f1.read(1) # one byte at a time
if x == eol: # ok we have one full line
break
else:
t += x # else concatenate into current line
print(t) # to control the initial 52 lines
while True:
t = bytes((0,)) # struct only knows how to process 4 bytes int
for i in range(3): # so build one starting with a null byte
t += f1.read(1)
# print(t)
if(len(t) == 1): break # reached end of file
if(len(t) < 4): # reached end of file with uncomplete value
print("Remaining bytes at end of file", t)
break
# the trick is that the integer division by 256 skips the initial 0 byte and keeps the sign
i = struct.unpack('<i', t)[0]//256 # // for Python 3, only / for Python 2
print(i, hex(i)) # or any other more useful processing
备注:以上代码假定您对 52 行(以行尾结束)的描述是正确的,但显示的图像让我们认为最后一行不是。那样的话,你应该先算51行,然后跳过最后一行的内容。
def skipline(fd, nlines, eol):
for i in range(nlines): # process nlines lines
t = b'' # to store the content of each line
while True:
x = fd.read(1) # one byte at a time
if x == eol: # ok we have one full line
break
else:
t += x # else concatenate into current line
# print(t) # to control the initial 52 lines
with open('/Users/anais/Documents/CR_lab/Lab_files/labtest.raw', mode = 'rb') as f1:
skiplines(f1, 51, b'\n') # skip 51 lines terminated with a \n
skiplines(f1, 1, b'>') # skip last line assuming it ends at the >
...
我有一个包含 52 行 html header 的 .raw 文件,后跟数据本身。该文件以 little-endian 24bits SIGNED 编码,我想将数据转换为 ASCII 文件中的整数。我用 Python 3.
我尝试 'unpack' 整个文件,并在 this post 中找到以下代码:
import sys
import chunk
import struct
f1 = open('/Users/anais/Documents/CR_lab/Lab_files/labtest.raw', mode = 'rb')
data = struct.unpack('<i', chunk + ('[=11=]' if chunk[2] < 128 else '\xff'))
但我收到此错误消息:
TypeError: 'module' object is not subscriptable
编辑
好像这样更好:
data = struct.unpack('<i','[=13=]'+ bytes)[0] >> 8
但我仍然收到错误消息:
TypeError: must be str, not type
我想很容易修复?
在 Python 中处理这不是一个很好的文件! Python 非常适合处理文本文件,因为它会在内部缓冲区中以大块的形式读取它们,然后逐行迭代,但是您无法轻松访问像这样读取文本后出现的二进制数据。此外,struct
模块不支持 24 位值。
我能想到的唯一方法是一次一个字节地读取文件,首先跳过 52 次行尾,然后一次读取 3 个字节,将它们连接成一个 4 字节的字节字符串并解压.
可能的代码可能是:
eol = b'\n' # or whatever is the end of line in your file
nlines = 52 # number of lines to skip
with open('/Users/anais/Documents/CR_lab/Lab_files/labtest.raw', mode = 'rb') as f1:
for i in range(nlines): # process nlines lines
t = b'' # to store the content of each line
while True:
x = f1.read(1) # one byte at a time
if x == eol: # ok we have one full line
break
else:
t += x # else concatenate into current line
print(t) # to control the initial 52 lines
while True:
t = bytes((0,)) # struct only knows how to process 4 bytes int
for i in range(3): # so build one starting with a null byte
t += f1.read(1)
# print(t)
if(len(t) == 1): break # reached end of file
if(len(t) < 4): # reached end of file with uncomplete value
print("Remaining bytes at end of file", t)
break
# the trick is that the integer division by 256 skips the initial 0 byte and keeps the sign
i = struct.unpack('<i', t)[0]//256 # // for Python 3, only / for Python 2
print(i, hex(i)) # or any other more useful processing
备注:以上代码假定您对 52 行(以行尾结束)的描述是正确的,但显示的图像让我们认为最后一行不是。那样的话,你应该先算51行,然后跳过最后一行的内容。
def skipline(fd, nlines, eol):
for i in range(nlines): # process nlines lines
t = b'' # to store the content of each line
while True:
x = fd.read(1) # one byte at a time
if x == eol: # ok we have one full line
break
else:
t += x # else concatenate into current line
# print(t) # to control the initial 52 lines
with open('/Users/anais/Documents/CR_lab/Lab_files/labtest.raw', mode = 'rb') as f1:
skiplines(f1, 51, b'\n') # skip 51 lines terminated with a \n
skiplines(f1, 1, b'>') # skip last line assuming it ends at the >
...