DonkeyMaster DonkeyMaster - 1 year ago 85
Python Question

Python regular expression parsing binary file

I have a file which mixes binary data and text data. I want to parse it through a regular expression, but I get this error:

TypeError: can't use a string pattern on a bytes-like object

I'm guessing that message means that Python doesn't want to parse binary files.
I'm opening the file with the

How can I parse binary files with regular expressions in Python?

EDIT: I'm using Python 3.2.0


In your re.compile you need to use a bytes object, signified by an initial b:

r = re.compile(b"(This)")

This is Python 3 being picky about the difference between strings and bytes.