python3 怎么把中文转成 unicode

2014-07-26 08:49:47 +08:00
 berry10086
python3里面的中文是str 请问怎么转换unicode 例如,中的unicode码是\u4e2d 怎么转换成字符串4e2d
7457 次点击
所在节点    问与答
8 条回复
PeterD
2014-07-26 09:43:34 +08:00
python3中'中文'str好像默认就是 unicode

>>>a = '是否'
>>>a[0]
'否'
>>>'\u4e2d'
'中'
PeterD
2014-07-26 09:44:38 +08:00
不好意思,手快了

>>>a = '是否'
>>>a[0]
'是'
>>>'\u4e2d'
'中'
berry10086
2014-07-26 10:50:08 +08:00
@PeterD 我意思是,中这个字 怎么转换成4e2d 这个字符串
goodxxx
2014-07-26 11:01:36 +08:00
"中".decode("utf-8")
lizheming
2014-07-26 11:01:43 +08:00
>>> "\u4e2d".encode('unicode-escape')
b'\\u4e2d'
reusFork
2014-07-26 11:01:43 +08:00
hex(ord('中'))[2:]
regsvr32
2014-07-26 11:04:02 +08:00
print u'中'.encode('unicode-escape')
berry10086
2014-07-28 14:00:14 +08:00
@reusFork 谢谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/124569

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX