首页 > 解决方案 > Python - 如何格式化字典但保持相对时间戳?

问题描述

我有一个像下面这样的字典,键是日期,字典中的项目是数字字符和时间。我想合并这些行,而不是丢失时间戳(它可以稍微四舍五入,但仍然应该代表小时和分钟是可能的)。我想从十进制捕获到十进制,但我不知道该怎么做。该字典已从文本文件中解析并包含在字典 offset_dict 中。

这是字典:

offset_dict = {'2019/07/15': ['. 11:47:24',
                             '0 11:47:24 ',
                             '0 11:47:24 ',
                             '0 11:47:24',
                             '1 11:47:24 ', 
                             '. 11:47:47',
                             '0 11:47:47',
                             '0 11:47:47',
                             '0 11:47:47',
                             '4 11:47:48',
                             '. 11:51:46',
                             '0 11:51:47',
                             '0 11:51:47',
                             '0 11:51:48',
                             '3 11:51:48'],
              '2019/07/16': ['. 06:24:52',
                             '1 06:24:53',
                             '0 06:24:53',
                             '2 06:24:56', 
                             '8 06:24:57', 
                             '0 06:24:57',
                             '0 06:24:59',
                             '8 06:24:59',
                             '0 06:24:59',
                             '8 06:25:00',
                             '0 06:25:03',
                             '. 06:25:04',
                             '5 06:25:04',
                             '0 06:25:05',
                             '. 06:34:19',
                             '0 06:34:19',
                             '0 06:34:19',
                             '5 06:34:35']}

运行这个:

for key, line in offset_dict.items():
    print(key)
    print(line)

将字典输出为

2019/07/15
{'. 11:47:24', '3 11:51:48', '1 11:47:24 ', '0 11:47:47', '0 11:47:24 ', '0 11:51:47', '. 11:47:47', '0 11:51:48', '4 11:47:48', '. 11:51:46', '0 11:47:24'}
2019/07/16
{'1 06:24:53', '0 06:25:03', '0 06:24:53', '8 06:24:57', '. 06:34:19', '0 06:25:05', '5 06:34:35', '8 06:24:59', '. 06:24:52', '8 06:25:00', '5 06:25:04', '2 06:24:56', '. 06:25:04', '0 06:34:19', '0 06:24:57', '0 06:24:59'}

我想看到的格式:

2019/07/15
{'.0001 11:47:00', '.0004 11:47:00', '.0003 11:51:00'}

2019/07/16
{'.1028008080 06:24:00' '.50 06:25:00', '.005 06:34:00'}

标签: pythondictionaryformatting

解决方案


不是最干净的解决方案,但只要顺序不重要并且使用时间戳不需要以00. 如果输出的顺序很重要,我需要稍微改变一下。

for date in offset_dict:
    times_for_date = []
    decimal_time = []
    start_time = None

    for entry in offset_dict[date]:
        start, time = entry.split()
        if start == '.':
            if start_time:
                times_for_date.append((''.join(decimal_time), start_time))
                decimal_time = []
            start_time = time
        decimal_time.append(start)
    if decimal_time:
        times_for_date.append((''.join(decimal_time), start_time))

    output_set = set(f"{decimal_time} {start_time}" for decimal_time, start_time in times_for_date)
    print(f"{date} {output_set}")

输出:

2019/07/15 {'.0004 11:47:47', '.0001 11:47:24', '.0003 11:51:46'}
2019/07/16 {'.50 06:25:04', '.005 06:34:19', '.1028008080 06:24:52'}

推荐阅读