Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove escape characters (escaping unicode chars) from unicode string in Python2.x?

>>> test
u'"Hello," he\u200b said\u200f\u200e.\n\t"I\u200b am\u200b nine years old\xe2"'
>>> test2
'"Hello," he\\u200b said\\u200f\\u200e.\n\t"I\\u200b am\\u200b nine years old"'
>>> print test
"Hello," he said‏‎.
        "I am nine years oldâ"
>>> print test2
"Hello," he\u200b said\u200f\u200e.
        "I\u200b am\u200b nine years old"

So how would I convert from test2 to test (i.e. so that unicode characters are printed)? .decode('utf-8') doesn't do it.

like image 654
kawakaze Avatar asked Jan 21 '26 02:01

kawakaze


1 Answers

You can use unicode-escape encoding to decode '\\u200b' to u'\u200b'.

>>> test1 = u'"Hello," he\u200b said\u200f\u200e.\n\t"I\u200b am\u200b nine years old\xe2"'
>>> test2 = '"Hello," he\\u200b said\\u200f\\u200e.\n\t"I\\u200b am\\u200b nine years old"'
>>> test2.decode('unicode-escape')
u'"Hello," he\u200b said\u200f\u200e.\n\t"I\u200b am\u200b nine years old"'
>>> print test2.decode('unicode-escape')
"Hello," he​ said‏‎.
    "I​ am​ nine years old"

Note: But even with that, test2 cannot be decoded to match exactly test1 because there's a u'\xe2' in test1 just before the closing quote (").

>>> test1 == test2.decode('unicode-escape')
False
>>> test1.replace(u'\xe2', '') == test2.decode('unicode-escape')
True
like image 108
falsetru Avatar answered Jan 23 '26 20:01

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!