python二进制文件解析_python – 解析二进制文件的正则表达式?

我认为你使用Python 3。

1.Opening a file in binary mode is simple but subtle. The only difference

from opening it in text mode is that

the mode parameter contains a ‘b’

character.

……..

4.Here’s one difference, though: a binary stream object has no encoding

attribute. That makes sense, right?

You’re reading (or writing) bytes, not

strings, so there’s no conversion for

Python to do.

07000

然后,在Python 3中,由于来自文件的二进制流是字节流,必须使用字节序列来定义从文件分析流的正则表达式,而不是字符串序列。

In Python 2, a string was an array of

bytes whose character encoding was

tracked separately. If you wanted

Python 2 to keep track of the

character encoding, you had to use a

Unicode string (u”) instead. But in

Python 3, a string is always what

Python 2 called a Unicode string —

that is, an array of Unicode

characters (of possibly varying byte

lengths).

07001

In Python 3, all strings are sequences

of Unicode characters. There is no

such thing as a Python string encoded

in UTF-8, or a Python string encoded

as CP-1252. “Is this string UTF-8?” is

an invalid question. UTF-8 is a way of

encoding characters as a sequence of

bytes. If you want to take a string

and turn it into a sequence of bytes

in a particular character encoding,

Python 3 can help you with that.

07002

4.6. Strings vs. Bytes# Bytes are bytes; characters are an abstraction.

An immutable sequence of Unicode

characters is called a string. An

immutable sequence of

numbers-between-0-and-255 is called a

bytes object.

….

1.To define a bytes object, use the b’ ‘ “byte literal” syntax. Each byte

within the byte literal can be an

ASCII character or an encoded

hexadecimal number from \x00 to \xff

(0–255).

07002

所以你将定义你的正则表达式如下

pat = re.compile(b'[a-f]+\d+')

而不是

pat = re.compile('[a-f]+\d+')

更多解释

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值