Skip to content Skip to sidebar Skip to footer

Python Re Module To Replace The Binary Data Inside A Text File?

I know mixing text and binary is awful, but I have to do this. I want to replace the binary content, which is around with 'Content-Type: image' and '----', by string 'XXXXXXXX' So

Solution 1:

This works for me:

data = re.sub(r"Content-Type: image.*-----","Content-Type: imageXXXXXXX-----", 
              raw_data, 0, re.DOTALL)

Essentially it matches in a greedy way all characters between Content-Type: image and -----. The 0 means "match all occurrences of this pattern". Probably this is superfluous for you, but you can't skip it as you also wanted to use the flag re.DOTALL that modify the meaning of "any characters" to also include newlines.

HTH!

Post a Comment for "Python Re Module To Replace The Binary Data Inside A Text File?"