【问题标题】:How do I convert a bitstring to an utf-8 string?如何将位串转换为 utf-8 字符串?
【发布时间】:2013-08-20 00:39:57
【问题描述】:

我使用bitstring 模块。将位串转换为 utf-8 字符串的最简单方法是什么?

【问题讨论】:

  • 您使用的是哪个版本的 Python?
  • @user2387370:我使用 python 2.7

标签: python string python-2.7 utf-8 bitstring


【解决方案1】:

您可以使用 BitVector 上的 .bytes 属性将向量中的位序列转换为 Python (2.7) str 对象形式的相同位序列。

因为strs 与编码无关,只要您存储在向量中的位序列对应于 UTF-8 编码的 Unicode 字符串,您就可以认为自己已经完成了。 (换句话说,使用这种方法,您可以在向量中存储与某些 UTF-8 编码的 Unicode 字符序列相对应的位。)

【讨论】:

    【解决方案2】:

    如位串模块文档中所述:

    # To covert to an ordinary string use the bytes property
    open('video.m2v', 'wb').write(s.bytes)
    

    信息可用here

    【讨论】:

      猜你喜欢
      • 2010-09-21
      • 2013-03-02
      • 1970-01-01
      • 2015-11-14
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      相关资源
      最近更新 更多