我需要使用Python 3解析终端输出命令
将整个输出按每行拆分:
systemctl list-units --type=service --all --no-pager
。我需要获取输出文本的每个单元格值。将整个输出按每行拆分:
text1 = subprocess.check_output("systemctl list-units --type=service --all --no-pager", shell=True).strip().decode()
text_split = text1.split("\n")
但每行都有空格,而且某些行的数据也有空格。使用.split(" ")
不能起作用。
我该怎么做?
操作系统:Debian-like Linux x64(Kernel 4.19)。
for i in text_split: i.split()
,它将提供每行数据项的列表。或者x = [i.split() for i in text_split]
将 x 给定为一个列表的列表。 - Rolf of Saxony' '.join(line.split()[3:])
。 - Pietro