为什么 `print foo.communicate()[0]` 与 `print foo.communicate()` 不同?

发布于 2024-08-28 13:32:16 字数 729 浏览 3 评论 0原文

情况是这样的:

我正在运行一个命令:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆躺在深渊里 2024-09-04 13:32:16

在 Python 中,当您调用诸如 print obj 之类的内容时,您实际上是在对象上调用 __str__ ,然后将结果解释为字符串。

例如,假设您有:

      >>> a = ('foo\nbar', '5')
      >>> print a
      ('foo\nbar', '5')

正如您所注意到的。但是,如果您执行以下操作:

      >>> class mytuple(tuple):
              def __str__(self):
                  return ''.join(self)
      >>> b = mytuple(a)
      >>> print b
      foo
      bar5

元组类型的这个子类超出了标准 __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:

      >>> a = ('foo\nbar', '5')
      >>> print a
      ('foo\nbar', '5')

As you've noticed. But if you do something like:

      >>> class mytuple(tuple):
              def __str__(self):
                  return ''.join(self)
      >>> b = mytuple(a)
      >>> print b
      foo
      bar5

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.

逆光飞翔i 2024-09-04 13:32:16

foo.communicate() 返回一个元组。如果您编写 foo.communicate()[0] 您将检索该元组中的第一个元素。由于这个元素只是一个字符串,Python 解释器将其打印到屏幕上,将 \n 打印为换行符。

当您从 foo.communinicate() 打印元组时,您会看到 \n 字符,但这只是元组打印方式的差异,换行符始终是那里。

foo.communicate() returns a tuple. If you write foo.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文