题
当我运行.exe文件它打印出来的东西到屏幕上。我不知道,我想打印出来,但有什么办法,我可以让Python一个写着“摘要”后打印下一行具体的线路?我知道这是在它打印那里当我需要之后的信息。谢谢!
解决方案
真的简单的Python溶液:
def getSummary(s):
return s[s.find('\nSummary'):]
此的第一实例之后返回一切的摘要强> 结果如果您需要更具体,我建议正则表达式。
其他提示
实际上
program.exe | grep -A 1 Summary
会做你的工作。
如果该exe打印到屏幕,然后通过管道将输出到文本文件中。我假定的exe是上的窗口,然后在命令行:
程序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()
不隶属于 StackOverflow