当我运行.exe文件时,它会将内容输出到屏幕上。我不知道我想要打印出来的具体行,但有没有办法让python在显示“摘要”之后打印下一行?我知道它在那里打印时我需要信息。谢谢!
答案 0 :(得分:3)
非常简单的Python解决方案:
def getSummary(s):
return s[s.find('\nSummary'):]
这会在第一个摘要实例后返回所有内容
如果你需要更具体,我建议使用正则表达式。
答案 1 :(得分:2)
实际上
program.exe | grep -A 1 Summary
会做你的工作。
答案 2 :(得分:1)
如果exe打印到屏幕然后管道输出到文本文件。我假设exe在Windows上,然后从命令行:
myapp.exe> output.txt的
你相当健壮的python代码就像:
try:
f = open("output.txt", "r")
lines = f.readlines()
# Using enumerate gives a convenient index.
for i, line in enumerate(lines) :
if 'Summary' in line :
print lines[i+1]
break # exit early
# Python throws this if 'Summary' was there but nothing is after it.
except IndexError, e :
print "I didn't find a line after the Summary"
# You could catch other exceptions, as needed.
finally :
f.close()