70

在 Python 3 中,可以格式化如下字符串:

"{0}, {1}, {2}".format(1, 2, 3)

但是如何格式化字节?

b"{0}, {1}, {2}".format(1, 2, 3)

提高AttributeError: 'bytes' object has no attribute 'format'

如果没有format字节的方法,如何对字节进行格式化或“重写”?

4

7 回答 7

42

从 Python 3.5 开始,%格式化也适用于bytes

这是Ethan Furman 撰写的PEP 461的一部分:

PEP: 461
Title: Adding % formatting to bytes and bytearray
Version: $Revision$
Last-Modified: $Date$
Author: Ethan Furman <ethan at stoneleaf.us>
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 2014-01-13
Python-Version: 3.5
Post-History: 2014-01-14, 2014-01-15, 2014-01-17, 2014-02-22, 2014-03-25,
               2014-03-27
Resolution:


Abstract
========

This PEP proposes adding % formatting operations similar to Python 2's ``str``
type to ``bytes`` and ``bytearray`` [1]_ [2]_.


Rationale
=========

While interpolation is usually thought of as a string operation, there are
cases where interpolation on ``bytes`` or ``bytearrays`` make sense, and the
work needed to make up for this missing functionality detracts from the overall
readability of the code.


Motivation
==========

With Python 3 and the split between ``str`` and ``bytes``, one small but
important area of programming became slightly more difficult, and much more
painful -- wire format protocols [3]_.

This area of programming is characterized by a mixture of binary data and
ASCII compatible segments of text (aka ASCII-encoded text).  Bringing back a
restricted %-interpolation for ``bytes`` and ``bytearray`` will aid both in
writing new wire format code, and in porting Python 2 wire format code.

Common use-cases include ``dbf`` and ``pdf`` file formats, ``email``
formats, and ``FTP`` and ``HTTP`` communications, among many others.

PEP 461 于2014 年 3 月 27 日被 Guido van Rossum 接受

公认。恭喜你组织了另一个颇有争议的讨论,并忍受了我最后一分钟的呆板!

由此,我们显然可以得出结论,%不再计划弃用(正如 Python 3.1 所宣布的那样)。

于 2014-03-28T00:06:29.500 回答
24

另一种方法是:

"{0}, {1}, {2}".format(1, 2, 3).encode()

在 IPython 1.1.0 和 Python 3.2.3 上测试

于 2014-01-09T13:20:18.443 回答
13

有趣的是.format(),似乎不支持字节序列;正如你所展示的。

您可以.join()按照此处的建议使用:http: //bugs.python.org/issue3982

b", ".join([b'1', b'2', b'3'])

BDFL 本人展示了与.join()过度使用相关的速度优势: http ://bugs.python.org/msg180449.format()

于 2013-03-29T20:05:28.550 回答
10

我发现%b在 Python 3.6.2 中效果最好,它应该适用于 b"" 和 "":

print(b"Some stuff %b. Some other stuff" % my_byte_or_unicode_string)
于 2017-11-09T16:39:41.730 回答
6

对于 Python 3.6+,您可以使用这种简洁明了的语法:

f'foo {bar}'.encode() # a byte string
于 2020-08-20T09:46:18.493 回答
-1

b|"你好 {0}!".format("世界")

#!/usr/bin/env python3.9
# -*- coding: utf-8 -*-

import builtins


class bytes(bytes):
    def format(self, *args, encoding: str = "utf-8") -> builtins.bytes:
        return_value: bytes = self

        for arg, i in zip(args, range(len(args))):
            pattern: bytes = f"{i}".encode(encoding=encoding)
            return_value = return_value.replace(pattern, bytes(arg, encoding=encoding))

        return return_value

    def __or__(self, other):
        return bytes(self + bytes(other, encoding="utf-8"))


b = bytes('', encoding="utf-8")


if __name__ == '__main__':   
    print( b|"Hello {0} {1}".format("World", "!") )
于 2021-08-26T01:27:32.160 回答
-2

我发现这行得通。

a = "{0}, {1}, {2}".format(1, 2, 3)

b = bytes(a, encoding="ascii")

>>> b
b'1, 2, 3'
于 2020-05-22T03:58:07.270 回答