Question

I'm trying to convert string1 so it would give the output as what's on string2

string1 = "<value>UK LONDON 8CS Flr01 Rm1\\xc4\\x9f</value>"

string2 = "<value>UK LONDON 8CS Flr01 Rm1ğ</value>"

I'm using plone with limited options, lets assume I cannot import any external libraries.

When I tried to use .decode( 'unicode-escape') it returns following string

print string1.decode( 'unicode-escape' )  # prints <value>UK LONDON 8CS Flr01 Rm1Ä</value>

Note that it takes Ä by converting first part of the unicode ( \xc4 )

Thanks

Was it helpful?

Solution

Use string-escape encoding instead of unicode-escape:

>>> print string1.decode('string-escape')
<value>UK LONDON 8CS Flr01 Rm1ğ</value>

or use decode it using unicode-escape, then encode it using latin1 encoding:

>>> print string1.decode('unicode-escape').encode('latin1')
<value>UK LONDON 8CS Flr01 Rm1ğ</value>

NOTE string-escape is only available in Python 2.x.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top