本文介绍了在 Python 3 中不带 b' 前缀的字节抑制/打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
只是发布这个以便我以后可以搜索它,因为它似乎总是难倒我:
Just posting this so I can search for it later, as it always seems to stump me:
$ python3.2
Python 3.2 (r32:88445, Oct 20 2012, 14:09:50)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import curses
>>> print(curses.version)
b'2.2'
>>> print(str(curses.version))
b'2.2'
>>> print(curses.version.encode('utf-8'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'bytes' object has no attribute 'encode'
>>> print(str(curses.version).encode('utf-8'))
b"b'2.2'"
问题:如何在 Python 3 中打印一个二进制 (bytes
) 字符串,没有 b'
前缀?
As question: how to print a binary (bytes
) string in Python 3, without the b'
prefix?
推荐答案
使用decode
:
print(curses.version.decode())
# 2.2
这篇关于在 Python 3 中不带 b' 前缀的字节抑制/打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!