最近转换战场,可能要很长一段时间在windows上耕耘。在python掉windows cmd命令时,发现返回的是一串乱码,如发送dir命令,返回如下:
b' xc7xfdxb6xafxc6xf7 D xd6xd0xb5xc4xbexedxc3xbbxd3xd0xb1xeaxc7xa9xa1xa3 xbexedxb5xc4xd0xf2xc1xd0xbaxc5xcaxc7 88E8-2AD2 D:\PyInvo xb5xc4xc4xbfxc2xbc 2021/07/26 13:35 <DIR>
即使使用最简单的执行echo。返回的也是一串带b''内容
b'xxx '
那么,这个b''是什么意思呢?
从官方文档中,我们看看
Firstly, the syntax for bytes literals is largely the same as that for string literals, except that a b prefix is added: Single quotes: b'still allows embedded "double" quotes' Double quotes: b"still allows embedded 'single' quotes". Triple quoted: b'''3 single quotes''', b"""3 double quotes"""
是一个bytes,序列,说明见链接https://docs.python.org/3/library/stdtypes.html#bytes
需要怎么处理呢?
最简单的,如果包含文本,需要用对应的编码方式进行解码
如:
strvalue = bytesvalue.decode('utf-8')
参考文档:
https://docs.python.org/3/library/stdtypes.html#bytes
https://docs.python.org/3/library/codecs.html#error-handlers
https://docs.python.org/3.3/reference/lexical_analysis.html#string-and-bytes-literals