简体   繁体   中英

Escaping unicode string using \u

I have a string like "válido" . Usually python can convert this to hex based easily on the command prompt and this would become 'v\\xc3\\x83\\xc2\\xa1lido'

But I want to use \\u\u003c/code> for the unicode codepoints, so I want the output like "v\Â\¡lido"

So basically the input should be "válido" and the output should be "v\Â\¡lido"

\\u\u003c/code> only works in Unicode strings; start your string literal with u :

u"v\u00c2\u00a1lido"

Demo:

>>> u"v\u00c2\u00a1lido"
u'v\xc2\xa1lido'
>>> print u"v\u00c2\u00a1lido"
v¡lido

I think json.dumps is what you need:

>>> s="válido"
>>> s
'v\xc3\x83\xc2\xa1lido'
>>> json.dumps(s)
'"v\\u00c3\\u00a1lido"'
>>> print json.dumps(s)
"v\u00c3\u00a1lido"

Maybe it's too late for the OP, but hope it can help guys who are trying to solve the same problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM