The reverse of encode('...', 'backslashreplace')

Tor Erik Sønvisen torerik81 at gmail.com
Mon Sep 3 15:32:04 EDT 2007


Hi,

How can I transform b so that the assertion holds? I.e., how can I
reverse the backslash-replaced encoding, while retaining the str-type?

>>> a = u'æ'
>>> b = a.encode('ascii', 'backslashreplace')
>>> b
'\\xe6'
>>> assert isinstance(b, str) and b == 'æ'

Traceback (most recent call last):
  File "<pyshell#59>", line 1, in <module>
    assert isinstance(b, str) and b == 'æ'
AssertionError

Regards,
tores



More information about the Python-list mailing list