I want this result:
u'\ue8fc\x82'
But it always gives me:
u'\\ue8fc\\u0082'
Sample 1:
>>> a='\ue8fc\u0082'
>>> a
'\\ue8fc\\u0082'
>>> print a
\ue8fc\u0082
>>> unicode(a)
u'\\ue8fc\\u0082'
>>> unicode(a).replace('\\\\','\\')
u'\\ue8fc\\u0082'
>>> repr(unicode(a).replace('\\\\','\\'))
"u'\\\\ue8fc\\\\u0082'"
>>> repr(unicode(b).replace('\\','?'))
"u'?ue8fc?u0082'"
>>> repr(unicode(b).replace('\\','?').replace('?','\\'))
"u'\\\\ue8fc\\\\u0082'"
Sample 2:
>>> u'\ue8fc\u0082'
u'\ue8fc\x82'
>>> repr(u'\ue8fc\u0082')
"u'\\ue8fc\\x82'"
Why I need this:
I wanna turn
'%ue8fc%u0082'
into
'\ue8fc\u0082'
The backslashes which are used to represent Unicode characters aren't literally part of the string, and cannot be manipulated using str.replace
. However, it's possible to convert a string with "real" backslashes into an escaped string using the "unicode_escape" encoding:
>>> s = "%ue8fc%u0082"
>>> s.replace("%", "\\").decode("unicode_escape")
u'\ue8fc\x82'