所以我需要先根据段落之间的新行将整个字符串分组为块,然后将状态为Error的组块分组。 然后我需要使用正则表达式匹配动作,如果这些动作匹配,那么我需要将字符串(块的第一行)追加到列表中。 对python和regex来说很新,任何帮助都会受到关注。
"""
Some string that has some meaning.
Doing Action1
Some error occurred which is logged
Reason for failures.
status: Error
Some other string that has some meaning
Doing Action2
Some other error occurred which is logged
Reason for failures.
status: Error
Some new string that has some meaning
Doing Action1
Action was done
Reason for Action.
status: Ok
Some brand new string that has some meaning
Doing Action2
Action was done
Reason for Action.
status: Error
"""
The result expected should be something like this when checking for Action1
[Some string that has some meaning.]
when checking for Action2:
[Some other string that has some meaning,
Some brand new string that has some meaning]
答案 0 :(得分:1)
如果您尝试按当前格式的文本块进行分组,则应执行以下操作:
text_split = text.split('\n\n')
文本是你写的输出(?)
这应该给你一个块列表。
如果您想要在新列表中过滤掉包含status: Error
的字符串块,您应该:
error_block = [x for x in text_split if 'status: Error' in x]