Python format string bytes

PEP 461 – Adding % formatting to bytes and bytearray

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.

Proposed semantics for bytes and bytearray formatting

%-interpolation

All the numeric formatting codes ( d , i , o , u , x , X , e , E , f , F , g , G , and any that are subsequently added to Python 3) will be supported, and will work as they do for str, including the padding, justification and other related modifiers (currently # , 0 , — , space, and + (plus any added to Python 3)). The only non-numeric codes allowed are c , b , a , and s (which is a synonym for b).

For the numeric codes, the only difference between str and bytes (or bytearray ) interpolation is that the results from these codes will be ASCII-encoded text, not unicode. In other words, for any numeric formatting code %x :

Читайте также:  Kotlin factory что это

Источник

Оцените статью