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
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com