首页 文章

如何从文本文件中的一行转换日期和时间?

提问于
浏览
2

我想知道是否有办法从文本文件的行中的预设值转换日期和时间 .

对于日期转换,我需要一年中的某一天 . 我找到了使用此代码获取当年当前日期的方法:

from datetime import datetime
day_of_year = datetime.now().timetuple().tn_yday
print day_of_year

但过去几年也需要它(例如,1991年,1995年,2004年) .

对于时间转换,我需要一天的秒数 . 我所拥有的将小时,分钟和秒转换为秒的代码是:

def get_sec(s):
    x = s.split(':')
    return int(x[0]) * 3600 + int(x[1]) * 60 + int(x[2])
    print get_sec('17:36:00) //gives me an output of 63360

但是我不能每次输入小时,分钟,秒的值,因为它必须从文本文件中的行读取任何值 .

我有的示例文本文件,我们称之为datetime.txt,是:

1.a  Date Installed :    1991-01-19T00:00Z
     Date Removed   :    1993-02-08T00:00Z

1.b  Date Installed :    1993-02-09T00:00Z
     Date Removed   :    1994-01-12T00:00Z

1.c  Date Installed :    1994-01-12T00:00Z
     Date Removed   :    1994-02-16T17:36Z

为了理解示例文本文件 for the time ,'T'之后的2个字符表示小时,冒号':'之后的2个字符表示分钟 . For the date ,短划线'-'之间的2个字符表示月份,'T'之前的2个字符表示天数 . 例如1.a, time 00是小时,00是分钟 . date 01是月(1月),19是天 .

我现在的代码是:

with open('datetime.txt', 'r') as dt:
for line in dt:
    header = line.split(':')[0]
    if 'Date Installed' in header:
        year = line.split(':')[1].strip()[2:4]
        day_of_year = line.split(':')[1].strip()[5:7] + line.split(':')[1].strip()[8:10]
        sec_of_day = line.split(':')[1].strip()[14:16]
        print year,
        print day_of_year,
        print sec_of_day

我添加的索引是告诉文本文件中的行中哪些字符打印,从头到尾 .

我已经被困在我脚本的这一部分了一段时间了 . 对Python来说仍然是新手,所以对所有事情都不熟悉 .

那么我如何实现或添加当前代码的日常和时间的两次转换?这样做的目的是让我可以运行一个通用代码,它将运行并为我提供相同的输出,格式和转换,以及具有不同值的其他文本文件 .

任何正确方向的帮助表示赞赏 .

Note 我不确定这个问题是否与其他内容重复 . 我不是很确定如何正确地说出我的问题,所以如果它最终成为重复,我会道歉 . 而且,这不适合学校 . 仅供我自己在工作中使用以运行不同的文本文件并提取/打印行的特定部分 .

3 回答

  • 0

    好吧,一旦你得到 1993-02-09T00:00Z 字符串,你就可以做到

    import time, datetime
    d = datetime.datetime.fromtimestamp(time.strptime('1993-02-09T00:00Z', '%Y-%m-%dT%H:%MZ'))
    

    它会给你一个 datetime 对象,你可以相应地使用它

    那么你可以做 d.yeard.hour 等事情 .

  • 1

    您的时间根据iso 8601规范进行格式化 .

    您可以使用dateutil来解析iso8601格式的日期时间 .

    data = """
    1.a  Date Installed :    1991-01-19T00:00Z
         Date Removed   :    1993-02-08T00:00Z
    
    1.b  Date Installed :    1993-02-09T00:00Z
         Date Removed   :    1994-01-12T00:00Z
    
    1.c  Date Installed :    1994-01-12T00:00Z
         Date Removed   :    1994-02-16T17:36Z
    """
    
    # Regular expression to find matches in the input data
    import re
    
    regex_pattern = re.compile(r"""
        (?P<key>\d+\.\w+).*?             # the key is <digits>.<letters>
        (?P<installed>[-:TZ0-9]{17}).*?  # the timestamps have length 17 
        (?P<removed>[-:TZ0-9]{17})
        """, 
        flags = re.VERBOSE | re.MULTILINE | re.DOTALL
    )
    

    正则表达式不是很复杂 . 详细标志和命名模式只是为了使其更具可读性 . 以下是表达式的详细说明:https://regex101.com/r/oT0rG3/1

    一旦我们分离了匹配,解析iso时间串就很简单了:

    from dateutil.parser import parse
    
    # Dictionary comprehension on the regex matches
    items = {
        key: tuple(parse(ts) for ts in timestamps)
        for key, *timestamps in regex_pattern.findall(data)
    }
    

    最终输出 items 是一本字典:

    {'1.a': (datetime.datetime(1991, 1, 19, 0, 0, tzinfo=tzutc()),
             datetime.datetime(1993, 2, 8, 0, 0, tzinfo=tzutc())),
     '1.b': (datetime.datetime(1993, 2, 9, 0, 0, tzinfo=tzutc()),
             datetime.datetime(1994, 1, 12, 0, 0, tzinfo=tzutc())),
     '1.c': (datetime.datetime(1994, 1, 12, 0, 0, tzinfo=tzutc()),
             datetime.datetime(1994, 2, 16, 17, 36, tzinfo=tzutc()))}
    
  • 0

    如果所有日期都采用相同的格式,则很容易从文件中提取它们(未经测试):

    #!/usr/bin/env python
    from datetime import datetime
    
    dates = {} # date -> label
    with open('datetime.txt') as file:
        for line in file:
            label, colon, date_string = line.partition(':')
            if colon:
                utc_time = datetime.strptime(date_string.strip(), '%Y-%m-%dT%H:%MZ')
                dates[utc_time] = label.strip()
    

相关问题