目录
[根据 Door opened 和Door closed 把字符串分组:](#根据 Door opened 和Door closed 把字符串分组:)
[如果最后一组没有Door closed,则丢弃最后一条数据:](#如果最后一组没有Door closed,则丢弃最后一条数据:)
根据 Door opened 和Door closed 把字符串分组:
python
log_text = """
[20231201-211102] Door opened! test_videos/save_results/
[20231201-211102] order_idx: 1/228, 23111004594831533_0_216.mp4
[20231201-211108] Door closed!
[20231201-211109] Door opened! test_videos/save_results/
[20231201-211109] order_idx: 2/228, order_path = 1338.mp4
[20231201-211115] Get: score 0.52009
[20231201-211116] Door closed!
[20231201-211116] Door opened! /test_videos/save_results/
[20231201-211116] order_idx: 3/228, order.video_paths = high_res_20231110-133918_SW0031423111005385940276_0_256.mp4
[20231201-211147] Get:score 0.988185
[20231201-211147] Put: core 0.988185
[20231201-211151] Get: re 0.525953
[20231201-211152] Door closed!
"""
pattern = re.compile(r'(?=.*Door opened!)(?=.*Door closed!).*\n')
segments = pattern.split(log_text)
# 移除空字符串
segments = [segment.strip() for segment in segments if segment.strip()]
如果最后一组没有Door closed,也会分成一组。
如果最后一组没有Door closed,则丢弃最后一条数据:
python
pattern = re.compile(r'Door opened!(.*?)Door closed!', re.DOTALL)
matches = pattern.findall(log_text)
# 打印匹配结果
for match in matches:
print(match.strip())
print("---------------------")