我想使用 python 3 从命令输出中捕获特定列

我想使用 python 3 从命令输出中捕获特定列

在 ubuntu 机器上,我想要捕获命令 "dpkg -l" 输出的第二列。我使用 python 3 并想要使用 subprocess 模块。以下命令:

fh=open("/tmp/test.out", 'wb')
with subprocess.Popen(["dpkg", "-l"], stdout=subprocess.PIPE) as proc:
   fh.write(proc.stdout.read())

返回如下:

ii yum 3.4.3-3 all Advanced front-end for rpm  
ii zeitgeist-core 1.0-0ubuntu4 amd64 event logging framework - engine  
ii zenity 3.24.0-1 amd64 Display graphical dialog boxes from shell scripts  
ii zenity-common 3.24.0-1 all Display graphical dialog boxes from shell scripts   
ii zip 3.0-11build1 amd64 Archiver for .zip files  
ii zita-ajbridge 0.7.0-1 amd64 alsa to jack bridge  
ii zita-at1 0.6.0-1 amd64 JACK autotuner  
ii zita-lrx 0.1.0-3 amd64 Command line jack application providing crossover filters  
ii zita-mu1 0.2.2-2 amd64 organise stereo monitoring for Jack Audio Connection Kit  
.....  
.....  

我想要获取第二列,例如:

....  
....  
yum  
zeitgeist-core  
zenity  
zenity-common  
zip  
....  
.... etc etc  

请帮忙

>>> with subprocess.Popen(["dpkg", "-l"], stdout=subprocess.PIPE) as proc:
...     line1=proc.stdout.read()
...     type(line1)
...
<class 'bytes'>

类型是字节。如何分割。当我使用以下内容时:

>>> with subprocess.Popen(["dpkg", "-l"], stdout=subprocess.PIPE) as proc:
...     line1=proc.stdout.read()
...     line2=str(line)  # the type is byte so I try to convert to string
...     print(line2)
...
10

(输出很混乱)

答案1

你获取系统调用输出的方法已经过时了。使用

subprocess.check_output()

反而:

#!/usr/bin/env python3
import subprocess

f = "/home/jacob/Desktop/output.txt"

lines = subprocess.check_output(["dpkg", "-l"]).decode("utf-8").splitlines()
with open(f, "wt") as out:
    for l in lines:
        if l.startswith("ii"):
            out.write(l.split()[1] + "\n")

替换f为输出文件的实际路径。

输出文件:

...
...
apg
app-install-data
app-install-data-partner
apparmor
apport
apport-gtk
apport-retrace
apport-symptoms
appstream
apt
apt-transport-https
...
...

笔记

上述解决方案将创建一个文件,以空行结尾。如果这是个问题,请使用下面的解决方案。

#!/usr/bin/env python3
import subprocess

f = "/home/jacob/Bureaublad/output.txt"

lines = subprocess.check_output(["dpkg", "-l"]).decode("utf-8").splitlines()
open(f, "wt").write(
    "\n".join([l.split()[1] for l in lines if l.startswith("ii")])
)

答案2

请注意,dpkg -l本质上是的前端dpkg-query,并dpkg-query允许您格式化输出。从man dpkg

dpkg-query actions
  See dpkg-query(1) for more information about the following actions.

  -l, --list package-name-pattern...
      List packages matching given pattern.

man dpkg-query

-l, --list [package-name-pattern...]
      List  packages  matching  given  pattern.
-W, --show [package-name-pattern...]
      Just like the --list option this will list all packages matching
      the  given  pattern.  However the output can be customized using
      the --showformat option.

因此,不要这样做dpkg -l,而要这样做:

dpkg-query -f '${Package}\n' -W

'${Package}'不是这里是 shell 变量。它是 的格式说明符dpkg-query

答案3

words=line1.split(" ")
print(words[1])

words 是一个字符串列表,其中 words[1] 就是您想要的。

相关内容