2009-06-09 113 views
0

當我運行一個.exe文件時,它將東西輸出到屏幕上。我不知道我想要打印的具體行,但有沒有辦法讓我可以在顯示「摘要」後打印下一行?我知道它在打印時就在那裏,我需要後面的信息。謝謝!Python和閱讀行

+0

你可以發佈一些示例代碼嗎?我不知道你是從Python運行一個EXE還是隻是想將一個字符串傳遞給一個Python腳本,或者你正在做其他事情。 – 2009-06-09 16:01:37

回答

3

真正簡單的Python的解決方案:

def getSummary(s): 
    return s[s.find('\nSummary'):] 

摘要
如果您需要更具體的初審後返回的一切,我建議正則表達式。

2

實際上

program.exe | grep -A 1 Summary 

會做你的工作。

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()