时间戳文本解析

如果文本中的每行时间戳已经是按照非降序排列的,那么我们可以简化上述脚本,因为我们不需要在每个时间点处检查时间戳的顺序。以下是针对这种情况的简化脚本:

```

python

from datetime import datetime

# 假设这是我们的输入文本,每行以时间戳开头,并且时间戳已经按非降序排列

text = """

[2022-01-01 13:00:00] Some text for the first period.

More text for the first period.

[2022-01-01 14:00:00] Text for the second period.

Even more text for the second period.

[2022-01-01 15:00:00] Text for the third period.

"""

# 给定的10个时间点,这里只是示例,需要替换为实际的时间点

time_points = [

    datetime.strptime('2022-01-01 13:30:00', '%Y-%m-%d %H:%M:%S'),

    datetime.strptime('2022-01-01 14:30:00', '%Y-%m-%d %H:%M:%S'),

    # ... 添加更多的时间点,直到10个

]

# 将输入文本按行分割

lines = text.strip().split('\n')

# 初始化变量

current_period_text = []

periods = {tp: [] for tp in time_points}  # 创建一个字典来保存不同时间段的内容

next_time_point_index = 0  # 下一个时间点的索引

# 遍历每一行

for line in lines:

    # 检查行首是否包含时间戳

    if line.startswith('['):

        # 解析新的时间戳

        timestamp_str = line.split(']')[0][1:]

        current_timestamp = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S')

       

        # 找到当前时间戳所属的时间段

        while next_time_point_index < len(time_points) and current_timestamp >= time_points[next_time_point_index]:

            next_time_point_index += 1

       

        # 将当前行添加到正确的时间段

        periods[time_points[next_time_point_index - 1]].append(line)

    else:

        # 如果行首不是时间戳,则将其添加到当前时间段

        periods[time_points[next_time_point_index - 1]].append(line)

# 打印或处理分割后的文本

for tp, period_text in periods.items():

    print(f"Period up to {tp}:")

    print('\n'.join(period_text))

    print()  # 打印空行以分隔不同的时间段

# 运行代码

# 输出将会是按时间段分隔的文本

```

在这个脚本中,我们使用了一个变量`next_time_point_index`来跟踪下一个时间点的索引。对于每一行,我们解析时间戳,并根据时间戳和`next_time_point_index`来确定当前行应该属于哪个时间段。如果当前行的时间戳大于或等于下一个时间点,我们就增加`next_time_point_index`。这样,我们就可以确保每行都被分配到正确的时间段。

请注意,这个脚本假设时间戳是按非降序排列的,并且每个时间点都是唯一的。如果你的时间点有重复或者时间戳没有排序,你需要先对时间点进行排序,并确保时间戳也是按顺序排列的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容