问题:如何在Python3中将“二进制字符串”转换为普通字符串?

例如,我有一个像这样的字符串(返回值subprocess.check_output):

>>> b'a string'
b'a string'

无论我对它做了什么,它总是b'在字符串之前印有烦人的字样:

>>> print(b'a string')
b'a string'
>>> print(str(b'a string'))
b'a string'

是否有人对如何将其用作普通字符串或将其转换为普通字符串有任何想法?

For example, I have a string like this(return value of subprocess.check_output):

>>> b'a string'
b'a string'

Whatever I did to it, it is always printed with the annoying b' before the string:

>>> print(b'a string')
b'a string'
>>> print(str(b'a string'))
b'a string'

Does anyone have any ideas about how to use it as a normal string or convert it into a normal string?


回答 0

解码它。

>>> b'a string'.decode('ascii')
'a string'

要从字符串获取字节,请对其进行编码。

>>> 'a string'.encode('ascii')
b'a string'

Decode it.

>>> b'a string'.decode('ascii')
'a string'

To get bytes from string, encode it.

>>> 'a string'.encode('ascii')
b'a string'

回答 1

如果来自falsetru的答案不起作用,您还可以尝试:

>>> b'a string'.decode('utf-8')
'a string'

If the answer from falsetru didn’t work you could also try:

>>> b'a string'.decode('utf-8')
'a string'

回答 2

请参阅图书馆的官方资料encode()decode()文档codecsutf-8是函数的默认编码,但是Python 3中有几种标准编码,例如latin_1utf_32

Please, see oficial encode() and decode() documentation from codecs library. utf-8 is the default encoding for the functions, but there are severals standard encodings in Python 3, like latin_1 or utf_32.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。