Python 跳过输出中没有任何起始行的行
Python skip the lines which do not have any of starting line in the output
在从 google 获得帮助后,我正在尝试编写代码,因此要解析命令输出但仍然遇到一些问题,因为输出是我期望连续的以 [=14= 开头的行] , instance
和 tag
但不知何故第一个输出只包含 dn
和 tag
所以,我想要那些没有所有这三个起始字符串的行然后跳过那些,正如我正在学习的那样,所以没有这样做的想法。
下面是我的代码:
import subprocess as sp
p = sp.Popen(somecmd, shell=True, stdout=sp.PIPE)
stout = p.stdout.read().decode('utf8')
output = stout.splitlines()
startline = ["instance:", "tag"]
for line in output:
print(line)
脚本输出:
dn: ou=People,ou=pti,o=pt
tag: pti00631
dn: cn=pti00857,ou=People,ou=pti,o=pt
instance: Jassu Lal
tag: pti00857
dn: cn=pti00861,ou=People,ou=pti,o=pt
instance: Gatti Lal
tag: pti00861
期望的输出:
dn: cn=pti00857,ou=People,ou=pti,o=pt
instance: Jassu Lal
tag: pti00857
dn: cn=pti00861,ou=People,ou=pti,o=pt
instance: Gatti Lal
tag: pti00861
假设您的输出始终相同,您的循环可能如下所示:
lines_to_skip = 3
skip_lines = False
skipped_lines = 0
for line in output():
if "dn: " in line and not "dn: cn" in line:
skip_lines = True
if skip_lines:
if skipped_lines < lines_to_skip:
skipped_lines += 1
continue
if skipped_lines == lines_to_skip:
skip_lines = False
skipped_lines = 0
print(line)
它将检查是否有一个 dn
没有 cn
,计数到 3(或者更确切地说 lines_to_skip
)并在到达要跳过的行时开始输出。
这是一个非常棘手的解决方案,但对于给定的上下文,这是我能想到的最好的解决方案
下面的代码是灵活的。您只需要在 necessary_tags
词典中添加您不想打印的标签。它也可以超过 3 个。它还考虑了您多次收到特定标签的情况。
import subprocess as sp
p = sp.Popen(somecmd, shell=True, stdout=sp.PIPE)
stout = p.stdout.read().decode('utf8')
output = stout.splitlines()
output.append("")
necessary_tags = {'dn':0, 'instance':0, 'tag':0}
temp_output = []
for line in (output):
tag = line.split(':')[0].strip()
if necessary_tags.get(tag, -1) != -1:
necessary_tags[tag] += 1
temp_output.append(line)
elif line == "":
if all(necessary_tags.values()):
for out in temp_output:
print(out)
temp_output = []
necessary_tags.update({}.fromkeys(necessary_tags,0))
print()
在从 google 获得帮助后,我正在尝试编写代码,因此要解析命令输出但仍然遇到一些问题,因为输出是我期望连续的以 [=14= 开头的行] , instance
和 tag
但不知何故第一个输出只包含 dn
和 tag
所以,我想要那些没有所有这三个起始字符串的行然后跳过那些,正如我正在学习的那样,所以没有这样做的想法。
下面是我的代码:
import subprocess as sp
p = sp.Popen(somecmd, shell=True, stdout=sp.PIPE)
stout = p.stdout.read().decode('utf8')
output = stout.splitlines()
startline = ["instance:", "tag"]
for line in output:
print(line)
脚本输出:
dn: ou=People,ou=pti,o=pt
tag: pti00631
dn: cn=pti00857,ou=People,ou=pti,o=pt
instance: Jassu Lal
tag: pti00857
dn: cn=pti00861,ou=People,ou=pti,o=pt
instance: Gatti Lal
tag: pti00861
期望的输出:
dn: cn=pti00857,ou=People,ou=pti,o=pt
instance: Jassu Lal
tag: pti00857
dn: cn=pti00861,ou=People,ou=pti,o=pt
instance: Gatti Lal
tag: pti00861
假设您的输出始终相同,您的循环可能如下所示:
lines_to_skip = 3
skip_lines = False
skipped_lines = 0
for line in output():
if "dn: " in line and not "dn: cn" in line:
skip_lines = True
if skip_lines:
if skipped_lines < lines_to_skip:
skipped_lines += 1
continue
if skipped_lines == lines_to_skip:
skip_lines = False
skipped_lines = 0
print(line)
它将检查是否有一个 dn
没有 cn
,计数到 3(或者更确切地说 lines_to_skip
)并在到达要跳过的行时开始输出。
这是一个非常棘手的解决方案,但对于给定的上下文,这是我能想到的最好的解决方案
下面的代码是灵活的。您只需要在 necessary_tags
词典中添加您不想打印的标签。它也可以超过 3 个。它还考虑了您多次收到特定标签的情况。
import subprocess as sp
p = sp.Popen(somecmd, shell=True, stdout=sp.PIPE)
stout = p.stdout.read().decode('utf8')
output = stout.splitlines()
output.append("")
necessary_tags = {'dn':0, 'instance':0, 'tag':0}
temp_output = []
for line in (output):
tag = line.split(':')[0].strip()
if necessary_tags.get(tag, -1) != -1:
necessary_tags[tag] += 1
temp_output.append(line)
elif line == "":
if all(necessary_tags.values()):
for out in temp_output:
print(out)
temp_output = []
necessary_tags.update({}.fromkeys(necessary_tags,0))
print()