문제

.exe 파일을 실행하면 내용이 화면에 인쇄됩니다.인쇄하려는 특정 줄을 모르지만 "요약"이라고 표시된 줄 다음에 Python이 다음 줄을 인쇄하도록 할 수 있는 방법이 있습니까?나는 그것이 인쇄될 때 거기에 있다는 것을 알고 있으며 바로 그 후에 정보가 필요합니다.감사해요!

도움이 되었습니까?

해결책

정말 간단한 파이썬 솔루션 :

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

이것은 첫 번째 인스턴스 후에 모든 것을 반환합니다 요약
더 구체적이어야한다면 정규 표현을 권장합니다.

다른 팁

실제로

program.exe | grep -A 1 Summary 

당신의 일을 할 것입니다.

exe가 화면에 인쇄되면 해당 출력을 텍스트 파일로 파이프합니다.exe가 Windows에 있다고 가정한 다음 명령줄에서 다음을 수행합니다.

myapp.exe > 출력.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()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top