Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; 'binary': 0.07; 'subject:file': 0.07; 'utf-8': 0.07; 'string': 0.09; 'interpreted': 0.09; 'occurrences': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'choose,': 0.16; 'guessing': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'replace': 0.24; 'file.': 0.24; 'script': 0.25; 'header:X-Complaints-To:1': 0.27; 'character': 0.29; 'thus': 0.29; 'file': 0.32; 'another': 0.32; 'possible.': 0.35; 'convert': 0.35; 'but': 0.35; 'wrong': 0.37; 'clear': 0.37; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; '8bit%:6': 0.40; 'how': 0.40; 'read': 0.60; 'is.': 0.60; 'skip:o 30': 0.61; 'subject:find': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: find and replace string in binary file Date: Tue, 04 Mar 2014 14:18:58 +0100 Organization: None References: <01951a7d-2ab3-4203-a9c5-2f79017a980d@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p57bd8024.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393939155 news.xs4all.nl 2835 [2001:888:2000:d::a6]:45960 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67672 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.