简体   繁体   English

python的字符串格式中的%a是什么

[英]what is %a in python's string formatting

I have a basic question regarding string formatting.我有一个关于字符串格式的基本问题。 I use both python 2.7 and 3+ for my coding我使用 python 2.7 和 3+ 进行编码

I understand that %s is used for characters or strings我知道 %s 用于字符或字符串

name = "John"
print("Hello, %s!" % name) 

Hello, John!你好约翰!

now, if I use an integer现在,如果我使用整数

name = 123
print("Hello, %s!" % name)

the output is Hello, 123!输出是你好,123!

And when I do want to convert it to bytes, something like this当我确实想将其转换为字节时,就像这样

name = 123
print(b"Hello, %s!" % name)

in python 2.7 it throws me an error saying that %b requires bytes, or an object that implements __bytes__, not 'int'在 python 2.7 中,它向我抛出一个错误,说%b requires bytes, or an object that implements __bytes__, not 'int'

name = 123
print(b"Hello, %a!" % name)

Is %a is explicitly used to take the bytes kind of stuff .. I couldn't find much info on %a on web.. and the old code which someone wrote extensively used it and I am debugging that .. SO need some help in understanding the above mentioned (using strings or numbers while converting to bytes ) %a 是否明确用于获取字节类型的东西.. 我在网络上找不到关于 %a 的太多信息.. 并且有人广泛使用的旧代码使用了它,我正在调试它.. 所以需要一些帮助理解上述内容(在转换为字节时使用字符串或数字)

%a will give the equivalent of repr(some_obj).encode('ascii', 'backslashreplace') on the interpolated value. %a 将在插值上给出相当于 repr(some_obj).encode('ascii', 'backslashreplace') 的值。 Use cases include developing a new protocol and writing landmarks into the stream;用例包括开发新协议并将地标写入流中; debugging data going into an existing protocol to see if the problem is the protocol itself or bad data;调试进入现有协议的数据,看看问题是协议本身还是坏数据; a fall-back for a serialization format;序列化格式的回退; or any situation where defining bytes would not be appropriate but a readable/informative representation is needed [6].或定义字节不合适但需要可读/信息性表示的任何情况[6]。

%r is included as a synonym for %a for the sole purpose of making 2/3 code bases easier to maintain. %r 作为 %a 的同义词包含在内,其唯一目的是使 2/3 代码库更易于维护。 Python 3 only code use %a Python 3 仅代码使用 %a

https://www.python.org/dev/peps/pep-0461/ https://www.python.org/dev/peps/pep-0461/

在打印语句中,您可以用 str(name).encode() 替换 name 以消除您遇到的错误。

%b is used for formatting a byte value in the print statement. %b用于格式化打印语句中的字节值。

s = "hello"
s_bytes = str.encode(s)
type(s_bytes)

class 'bytes' “字节”类

s_bytes

b'hello'再见

print(b"The text is %b" % s_bytes)

b'The text is hello' b'文本是你好'

print(b"The text is %b" % s)

Traceback (most recent call last): File "", line 1, in TypeError: %b requires a bytes-like object, or an object that implements bytes , not 'str'回溯(最近一次调用最后一次):文件“”,第 1 行,类型错误:%b 需要一个类似字节的对象,或实现bytes的对象,而不是 'str'

The bottom line is, you have to pass a byte object when you are formatting with %b .最重要的是,当您使用%b进行格式化时,您必须传递一个字节对象。

'%a' string format operator. '%a' 字符串格式运算符。 '%a' converts any python object to a string using repr() and then hex-escapes all non-ASCII characters. '%a' 使用 repr() 将任何 python 对象转换为字符串,然后对所有非 ASCII 字符进行十六进制转义。 The '%a' format operator generates the same string as '%r' in Python 2. Also, add '!a' conversion flags to the string.format() method and add '%A' operator to the '%a' 格式运算符生成与 Python 2 中的 '%r' 相同的字符串。此外,将 '!a' 转换标志添加到 string.format() 方法并将 '%A' 运算符添加到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM