简体   繁体   English

从特定字节读取二进制文件

[英]Reading a binary file from a specific byte

I'm trying to read a binary file from a specific byte (143) until another specific byte (150).我正在尝试从特定字节(143)读取二进制文件,直到另一个特定字节(150)。

I'm running this command:我正在运行这个命令:

tail -c +143 full_file.dat | head -c 6 | od -t x1
0000000 20 04 08 13 06 37
0000006

I have the values I want ( 20 04 08 13 06 ).我有我想要的值( 20 04 08 13 06 )。

But, trying the same (at least I'm thinking that) using Python, I'm getting this:但是,使用 Python 尝试相同(至少我是这么想的),我得到了这个:

'\x04\x08\x13\x067+'

The command I'm running in Python is:我在 Python 中运行的命令是:

g = open('full_file.DAT', 'rb')
g.seek(143, 1)
g.read(6)

What am I doing wrong here?我在这里做错了什么?

You could do something like this using Numpy:您可以使用 Numpy 执行以下操作:

import Numpy as np

arr = np.fromfile('full_file.DAT', dtype='uint8')
arr[143:143+6] #==> [20 04 08 13 06 37]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM