Python 为什么 Popen.communicate() 返回 b'hi\n' 而不是 'hi'?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15374211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does Popen.communicate() return b'hi\n' instead of 'hi'?
提问by imagineerThat
Can someone explain why the result I want, "hi", is preceded with a letter 'b' and followed with a newline?
有人可以解释为什么我想要的结果“嗨”前面是字母 'b',后面是换行符?
I am using Python 3.3
我正在使用Python 3.3
>>> import subprocess
>>> print(subprocess.Popen("echo hi", shell=True,
stdout=subprocess.PIPE).communicate()[0])
b'hi\n'
This extra 'b' does not appear if I run it with python 2.7
如果我使用 python 2.7 运行它,则不会出现这个额外的“b”
采纳答案by Necrolyte2
The echo command by default returns a newline character
echo 命令默认返回一个换行符
Compare with this:
与此比较:
print(subprocess.Popen("echo -n hi", \
shell=True, stdout=subprocess.PIPE).communicate()[0])
As for the bpreceding the string it indicates that it is a byte sequence which is equivalent to a normal string in Python 2.6+
至于字符串前面的b表示它是一个字节序列,相当于 Python 2.6+ 中的普通字符串
http://docs.python.org/3/reference/lexical_analysis.html#literals
http://docs.python.org/3/reference/lexical_analysis.html#literals
回答by zigg
The bindicates that what you have is bytes, which is a binary sequence of bytes rather than a string of Unicode characters. Subprocesses output bytes, not characters, so that's what communicate()is returning.
Theb表示您拥有的是bytes,它是一个二进制字节序列,而不是一串 Unicode 字符。子处理输出字节,而不是字符,所以这communicate()就是返回的内容。
The bytestype is not directly print()able, so you're being shown the reprof the bytesyou have. If you know the encoding of the bytes you received from the subprocess, you can use decode()to convert them into a printable str:
该bytes类型不是直接print()能,所以你正在显示repr的bytes你。如果您知道从子进程收到的字节的编码,则可以使用decode()将它们转换为可打印的str:
>>> print(b'hi\n'.decode('ascii'))
hi
Of course, this specific example only works if you actually are receiving ASCII from the subprocess. If it's not ASCII, you'll get an exception:
当然,此特定示例仅在您实际从子进程接收 ASCII 时才有效。如果它不是 ASCII,你会得到一个例外:
>>> print(b'\xff'.decode('ascii'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0…
The newline is part of what echo hihas output. echo's job is to output the parameters you pass it, followed by a newline. If you're not interested in whitespace surrounding the process output, you can use strip()like so:
换行符是echo hi输出的一部分。 echo的工作是输出你传递给它的参数,后跟一个换行符。如果您对进程输出周围的空格不感兴趣,可以strip()像这样使用:
>>> b'hi\n'.strip()
b'hi'
回答by Jenish
b is the byte representation and \n is the result of echo output.
b 是字节表示,\n 是 echo 输出的结果。
Following will print only the result data
以下将仅打印结果数据
import subprocess
print(subprocess.Popen("echo hi", shell=True,stdout=subprocess.PIPE).communicate()[0].decode('utf-8').strip())
回答by Danil
As mentioned before, echo hiactually does return hi\n, which it is an expected behavior.
如前所述,echo hi实际上确实 return hi\n,这是预期的行为。
But you probably want to just get the data in a "right" format and not deal with encoding. All you need to do is pass universal_newlines=Trueoption to subprocess.Popen()like so:
但是您可能只想以“正确”的格式获取数据,而不是处理编码。您需要做的就是传递universal_newlines=True选项来subprocess.Popen()喜欢这样:
>>> import subprocess
>>> print(subprocess.Popen("echo hi",
shell=True,
stdout=subprocess.PIPE,
universal_newlines=True).communicate()[0])
hi
This way Popen()will replace these unwanted symbols by itself.
这种方式Popen()将自行替换这些不需要的符号。

