Reputation: 481
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
for the unicode codepoints, so I want the output like "v\u00c2\u00a1lido"
So basically the input should be "válido"
and the output should be "v\u00c2\u00a1lido"
Upvotes: 1
Views: 206
Reputation: 7255
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.
Upvotes: 0
Reputation: 1121654
\u
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
Upvotes: 1