简体   繁体   English

将 unicode 格式字符串转换为实际的 utf 字符串

[英]Converting a unicode format string into actual utf string

When I call an api, I get an output like this- \\\य\\\ह \\\उ\\\द\\\ा\\\ह\\\र\\\ण \\\प\\\ा\\\ठ当我调用 api 时,我得到这样的输出 - \\\य\\\ह \\\उ\\\द\\\ा\\\ह\\\र\\\ण \\\प\\\ा\\\ठ

The utf encoding is stored as a string. utf 编码存储为字符串。 So string[1] here would be 'u'.所以这里的 string[1] 将是 'u'。 Anyone know how do I convert this into actual utf-8 string using python?任何人都知道如何使用 python 将其转换为实际的 utf-8 字符串?

You can use the codecs module:您可以使用codecs模块:

>>> s = "\\u092f\\u0939 \\u0909\\u0926\\u093e\\u0939\\u0930\\u0923 \\u092a\\u093e\\u0920"
>>> print(s)
\u092f\u0939 \u0909\u0926\u093e\u0939\u0930\u0923 \u092a\u093e\u0920

So:所以:

>>> import codecs
>>> codecs.decode(s, 'unicode-escape')
'यह उदाहरण पाठ'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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