On 1/9/2014 6:25 PM, Chris Barker wrote: > as so -- I want to replace a bit of ascii text surrounded by arbitrary > binary: > (apologies for the py2...) > In [24]: b > Out[24]: '\x01\x00\xd1\x80\xd1a name\xd0\x80' > In [25]: u = b.decode('latin-1') > In [26]: u2 = u.replace('a name', 'a different name') > In [28]: b2 = u2.encode('latin-1') > In [29]: b2 > Out[29]: '\x01\x00\xd1\x80\xd1a different name\xd0\x80' Just to check, with 3.4 print(b'\x01\x00\xd1\x80\xd1a name\xd0\x80' .decode('latin-1'). replace('a name', 'a different name') .encode('latin-1') == b'\x01\x00\xd1\x80\xd1a different name\xd0\x80') >>> True The b prefix works in 2.6/7, so this code does the same thing in 2.6+ and 3.x. -- Terry Jan Reedy
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4