Python 和阅读行

发布于 2024-07-23 10:53:33 字数 109 浏览 4 评论 0原文

当我运行 .exe 文件时,它会将内容打印到屏幕上。 我不知道我想要打印的具体行,但是有没有办法让 python 打印“Summary”之后的下一行? 我知道打印时就在那里,之后我需要信息。 谢谢!

When i run an .exe file it prints stuff out to the screen. I don't know the specific line of where i want printed out but is there a way I can get python to print the next line after one that says "Summary" ? I know that is in there when it prints and I need the info right after. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2024-07-30 10:53:33

非常简单的 Python 解决方案:

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

这将返回 Summary 第一个实例之后的所有内容

如果您需要更具体,我建议使用正则表达式。

Really simple Python solution:

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

This returns everything after the first instance of Summary

If you need to be more specific, I'd recommend regular expressions.

你是我的挚爱i 2024-07-30 10:53:33

实际上

program.exe | grep -A 1 Summary 

会做你的工作。

actually

program.exe | grep -A 1 Summary 

would do your job.

度的依靠╰つ 2024-07-30 10:53:33

如果 exe 打印到屏幕,则将该输出通过管道传输到文本文件。 我假设 exe 在 Windows 上,然后从命令行:

myapp.exe > 输出.txt

类似于:

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

If the exe prints to screen then pipe that output to a text file. I have assumed the exe is on windows, then from the command line:

myapp.exe > output.txt

And your reasonably robust python code would be something like:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文