py3 中文unicode转bytes

Enter空格 2017-02-16 12:04:17
如题。
python3 str默认是unicode的。
中文两字的unicode码是\u4e2d\u6587",对应的bytes应该是b"\x4e\x2d\x65\x87"
请问这个应该怎样转换?
...全文
238 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
混沌鳄鱼 2017-02-16
  • 打赏
  • 举报
回复
直接 byte_str = '中文'.encode('utf8' ) 就可以了
Enter空格 2017-02-16
  • 打赏
  • 举报
回复
引用 1 楼 xpresslink 的回复:
直接 byte_str = '中文'.encode('utf8' ) 就可以了
这个是转成utf8,我要的是unicode源字节码
Enter空格 2017-02-16
  • 打赏
  • 举报
回复
呃哦。。找到了 '中文'.encode("unicode_internal")

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧