unicode question

Kent Johnson kent at kentsjohnson.com
Sat Feb 25 10:37:08 EST 2006


Edward Loper wrote:
> I would like to convert an 8-bit string (i.e., a str) into unicode,
> treating chars \x00-\x7f as ascii, and converting any chars \x80-xff
> into a backslashed escape sequences.  I.e., I want something like this:
> 
>  >>> decode_with_backslashreplace('abc \xff\xe8 def')
> u'abc \\xff\\xe8 def'

  >>> s='abc \xff\xe8 def'
  >>> s.encode('string_escape')
'abc \\xff\\xe8 def'
  >>> unicode(s.encode('string_escape'))
u'abc \\xff\\xe8 def'

Kent



More information about the Python-list mailing list