【发布时间】:2016-05-12 19:48:35
【问题描述】:
在 python 3 中,我有字符串 u'\xf4\xfb\xe2'(unicode)。我需要将此字符串转换为字节 b'\xf4\xfb\xe2' (即 u'\xf4' -> b'\xf4' 等)。 我可以在 Python 2 中使用:
''.join([chr(ord(c)) for c in u'\xf4\xfb\xe2'])
但在 Python 3 中,chr() 返回 unicode。 我如何在 Python 3 中做到这一点?
【问题讨论】:
标签: python-3.x