为什么 `print foo.communicate()[0]` 与 `print foo.communicate()` 不同?
情况是这样的:
我正在运行一个命令:
import subprocess
foo = subprocess.Popen('ls /', shell=True, stdout=subprocess.PIPE,\
stderr=subprocess.STDOUT)
非常基本,对吧?我发现我可以使用 .communicate()
来处理输出,如下所示:
print foo.communicate()
效果很好,并生成 subprocess.communicate 文档建议的输出,一个元组:
('bin\nboot\ncdrom\n[...stuff redacted for brevity...]tmp\nusr\nvar\nvmlinuz\n', None)
注意其中的 \n
换行符。我发现仅要求元组的第一个元素就会产生带有换行符的输出,如下所示:
>>> print foo.communicate()[0]
bin
boot
cdrom
[...stuff redacted for brevity...]
tmp
usr
var
vmlinuz
但我不明白的是为什么仅使用第一个元素进行打印会产生换行符。不要误会我的意思,这很棒,我很高兴我可以在没有循环的情况下完成它,但我想了解发生了什么。
Here is the situation:
I've got a command I'm running:
import subprocess
foo = subprocess.Popen('ls /', shell=True, stdout=subprocess.PIPE,\
stderr=subprocess.STDOUT)
Pretty basic, right? And I've figured out that I can do stuff with the output with .communicate()
, like this:
print foo.communicate()
Which works great and produces the output that the documentation for subprocess.communicate suggests it should, a tuple:
('bin\nboot\ncdrom\n[...stuff redacted for brevity...]tmp\nusr\nvar\nvmlinuz\n', None)
Notice the \n
newlines in there. And I've discovered that just asking for the first element of the tuple produces output with newlines, like this:
>>> print foo.communicate()[0]
bin
boot
cdrom
[...stuff redacted for brevity...]
tmp
usr
var
vmlinuz
But what I don't understand is WHY printing with just the first element produces the newlines. Don't get me wrong, it is great, and I'm glad I can do it without a loop, but I'd like to understand what is going on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Python 中,当您调用诸如
print obj
之类的内容时,您实际上是在对象上调用__str__
,然后将结果解释为字符串。例如,假设您有:
正如您所注意到的。但是,如果您执行以下操作:
元组类型的这个子类超出了标准
__str__
并构建了一个由其组成部分的字符串表示形式组成的字符串。在一般情况下不这样做的原因是您永远不知道可以在元组或列表中插入哪些对象 - 并非所有对象都具有可字符串表示。
In Python when you call something like
print obj
, you're actually calling the__str__
on the object and then interpreting the result as a string.So for example say you have:
As you've noticed. But if you do something like:
This subclass of the tuple type goes beyond the standard
__str__
and builds a string composed out the string representations of its constituent parts.The reason this isn't done in the general case is that you never know what objects could be inserted in a tuple, or list - not all objects have string-able representations.
foo.communicate()
返回一个元组
。如果您编写foo.communicate()[0]
您将检索该元组中的第一个元素。由于这个元素只是一个字符串,Python 解释器将其打印到屏幕上,将\n
打印为换行符。当您从 foo.communinicate() 打印元组时,您会看到
\n
字符,但这只是元组打印方式的差异,换行符始终是那里。foo.communicate()
returns atuple
. If you writefoo.communicate()[0]
you retrieve a first element in that tuple. Since this element is just a string, python interpreter prints it to your screen, printing\n
as newlines.When you print a tuple from
foo.communinicate()
you see the\n
characters, but this is just a difference in the way the tuples are printed, the newlines are always there.