如何“二进制字符串”转换成普通字符串Python3? [英] How to convert 'binary string' to normal string in Python3?
本文介绍了如何“二进制字符串”转换成普通字符串Python3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我有一个这样的字符串(返回值 subprocess.check_output
)
For example, I have a string like this(return value of 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'
有没有人有关于如何使用它作为一个正常的字符串或将其转换成一个正常的字符串的任何想法?
Does anyone have any ideas about how to use it as a normal string or convert it into a normal string?
推荐答案
德code吧。
>>> b'a string'.decode('ascii')
'a string'
要得到字符串的字节,连接code吧。
To get bytes from string, encode it.
>>> 'a string'.encode('ascii')
b'a string'
这篇关于如何“二进制字符串”转换成普通字符串Python3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文