【问题标题】:Format string with bytes用字节格式化字符串
【发布时间】:2020-01-24 04:33:56
【问题描述】:

有一些我不明白的地方:此代码适用于 python3.5,但它在 python3.4 上给我一个错误:

s='abcd'
s2=b'%s' % s.encode('ascii')

这是错误:

TypeError: unsupported operand type(s) for %: 'bytes' and 'bytes'

知道为什么它适用于python3.5 而不适用于python3.4 吗?

【问题讨论】:

    标签: python python-3.x python-3.5


    【解决方案1】:

    在 Python 3.5 中使用 PEP 461 向字节和字节数组添加了对 % 格式的支持:

    此 PEP 建议将类似于 Python 2 的 str 类型的 % 格式化操作添加到 bytesbytearray

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-22
      • 1970-01-01
      相关资源
      最近更新 更多