0

嗨,我是 python 新手,真的不知道从哪里开始,所以这里

我有一个文本文件的以下部分,该文件要长得多,如果文件中的一行以“错误”开头,我想从中挑选一些信息,那么我想在另一个文本文件中打印该信息

1 warning message 
Warning 1 - Loop End (SGLoopControl2) - errors were encountered during run
2 error messages
Error 1 - 0952 RHFDShaftTorq Not Found in asd22961ck99_
Error 2 - Multi-column property values processing halted at loop iteration 2 (run failed)

非常感谢任何帮助。

4

1 回答 1

4
with open('path_to_file', 'rb') as f:
    for line in f:
        if line.startswith('Error'):
            do_something_with_line(line)

You might also want to strip the line with line.strip().

于 2013-07-07T21:15:36.267 回答