Python 3 字节格式 [英] Python 3 bytes formatting

查看:34
本文介绍了Python 3 字节格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Python 3, one can format a string like:

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

但是如何格式化字节?

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

引发 AttributeError: 'bytes' 对象没有属性 'format'.

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

If there is no format method for bytes, how to do the formatting or "rewriting" of bytes?

推荐答案

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

And as of 3.5 % formatting will work for bytes, too!

https://mail.python.org/pipermail/python-dev/2014-March/133621.html

这篇关于Python 3 字节格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆