find and replace string in binary file

Peter Otten __peter__ at web.de
Tue Mar 4 08:18:58 EST 2014


loial wrote:

> How do I read a binary file, find/identify a character string and replace
> it with another character string and write out to another file?
> 
> Its the finding of the string in a binary file that I am not clear on.

That's not possible. You have to convert either binary to string or string 
to binary before you can replace. Whatever you choose, you have to know the 
encoding of the file. Consider

#python3
ENCODING = "iso-8859-1"
with open(source, encoding=ENCODING) as infile:
    data = infile.read()
with open(dest, "w", encoding=ENCODING) as outfile:
    outfile.write(data.replace("nötig", "möglich"))

If the file is indeed iso-8859-1 this will replace occurrences of the bytes

b'n\xf6tig' with b'm\xf6glich'

But if you were guessing wrong and the file is utf-8 it may contain the 
bytes b'n\xc3\xb6tig' instead which are incorrectly interpreted by your 
script as 'nötig' and thus left as is.





More information about the Python-list mailing list