python3 unicode_Python3中如何得到Unicode码对应的中文?

2015-05-31

Ongoing-Study/ucps.py at master · myd7349/Ongoing-Study · GitHub

----

2015-06-03

我之前贴的代码具有误导性。因为我的代码实际上是在 raw string literal 和 str 转换。即:

ucps_to_str(r'\u8bf7') # 注意这里是一个 raw string literal,即实际上不是 '\u8bf7' 而是 '\\u8bf7'

->

对不起!

如果是 Python 3,我赞同 @竺夏栋 的回答:先看一下 text 的 type。如果是 str,应该是不需要转码(Python documentation:Textual data in Python is handled with str objects, or strings. Strings are immutable sequences of Unicode code points.)。如果是 bytes,则转之。

>>> '\u8bf7'

'请'

>>> b'\u8bf7'

b'\\u8bf7'

>>> b'\u8bf7'.decode('unicode-escape')

'请'

>>> b'\u8bf7'.decode('unicode_escape')

'请'

>>> r'\u8bf7' # 我写的脚本是处理这种情况的

'\\u8bf7'

>>> ucps_to_str(r'\u8bf7')

'请'

>>> '请'.encode('unicode-escape')

b'\\u8bf7'

>>> _.decode('unicode-escape')

'请'

--

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值