【问题标题】:Convert faulty unicode string to bytes将错误的 unicode 字符串转换为字节
【发布时间】: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


    【解决方案1】:

    试试这个:

    bytes(map(ord, u'\xf4\xfb\xe2'))
    

    【讨论】:

      猜你喜欢
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多