0

.exe ファイルを実行すると、画面に内容が出力されます。印刷したい特定の行がわかりませんが、「概要」と書かれた行の次の行をPythonに印刷させる方法はありますか? 印刷時にそこにあることはわかっており、すぐに情報が必要です。ありがとう!

4

3 に答える 3

3

本当にシンプルな Python ソリューション:

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

これは、 Summary の最初のインスタンス以降のすべてを返します
。より具体的にする必要がある場合は、正規表現をお勧めします。

于 2009-06-09T16:06:18.397 に答える
2

実際に

program.exe | grep -A 1 Summary 

あなたの仕事をします。

于 2009-06-09T16:02:18.443 に答える
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()
于 2009-06-10T13:13:27.363 に答える