python - python2中如何将str类型的unicode字符串转换为真正的unicode字符串?
问题描述
我正在尝试将 str 字符串转换'\u4e2d\u56fd'
为 unicode u'\u4e2d\u56fd'
。在python3中也是一样的,但是在python2.7中怎么做呢?
我尝试过 decode、encode、str、bytes、unicode 等,但它们都不起作用。
a = '\u4e2d\u56fd'
b = u'\u4e2d\u56fd'
print a
print b
上面代码的结果是
\u4e2d\u56fd
中国
我要做的就是将a转换为b。
感谢您的提示!
解决方案
您需要使用 raw_unicode_escape 编解码器。例如:
a = '\u4e2d\u56fd'
a = a.decode('raw_unicode_escape')
print a
中国
推荐阅读
- mysql - 数据库,每个用户一张表还是一张大表?
- python - Python:复杂的迭代
- python - 在 Sage 中将数字列表转换为 q-ary 表示
- sql - 为什么 EXEC sp_sqlexec @strSQL 与 EXECUTE (PRINT @strSQL) 不同
- r - 根据最小距离将点分配给组
- vbscript - 如何在 VBScript 中使用 %username%
- python - TypeError: unsupported operand type(s) for *: 'function' and 'int' in Python, 无法计算解决方案
- python - 令牌身份验证在 Django Rest 框架上不起作用
- excel - Excel VBA - 复制数组中的单元格并粘贴到下一个空单元格
- javascript - 如何区分我可以处理的错误和 Promise 中的程序员错误?