>>> command_stdoutb'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file2\n' ! r: ^! O' l% l9 s9 G- a, M% n
但是,我想用输出作为普通 Python 字符串。这样我就可以这样打印了:! ]9 ]( t8 i3 u: y B
>>> print(command_stdout)-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file1-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file23 I! J7 U) K( g. H4 d3 ^, W
我想这就是binascii.b2a_qp()使用方法,但当我尝试它时,我得到了相同的字节数组: + a! O( |4 Y" o% Y2 d
>>> binascii.b2a_qp(command_stdout)b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file2\n'0 T2 N9 [! m' e0 N
1 J, ~2 r5 j6 I. z8 C解决方案: 1 b3 Y& ?" _2 G
您需要解码 bytes 对象生成字符串:7 u. G( _- h( Y1 s0 I. U
>>> b"abcde"b'abcde'# utf-8 is used here because it is a very common encoding,but you# need to use the encoding your data is actually in.>>> b"abcde".decode("utf-8") 'abcde'* G+ T, ]6 U0 Z