Parse hours without leading zeroes by strptime in Python

前端 未结 3 874
死守一世寂寞
死守一世寂寞 2021-01-20 11:39

Suppose you have time in this format:

a = [..., 800.0, 830.0, 900.0, 930.0, 1000.0, 1030.0, ...]

The problem is that leading zeroes for hou

相关标签:
3条回答
  • 2021-01-20 12:08

    If those are really float literals in there vs strings, you can do this:

    a=[800., 830., 900., 930., 1000., 1030.]
    hours=[time.strptime('{:04.0f}'.format(f), '%H%M') for f in a]
    

    That will round the decimal if any (1033.66666 would be 1034 hence becoming 10:34 AM)

    You can also truncate like so:

    [800.0, 830.0, 900.0, 930.0, 1000.0, 1030.0, 1033.3333333, 1033.66666]
    hours=[time.strptime(str(f).split('.')[0], '%H%M') for f in a]
    

    edit from comments

    If you have values outside the range, you do this:

    a=[800., 830., 900., 930., 1000., 1030., 2400.]
    hours=[time.strptime(s,'%H%M') for s in ['{:04.0f}'.format(f) if f <2400 else '0000' for f in a]]
    

    or, you can make your original code work that way as well:

    [time.strptime(i,'%H%M') for i in[str(int(f)) if f<2400 else '0000' for f in a]]
    
    0 讨论(0)
  • 2021-01-20 12:18

    Use zfill to add those zeros back as needed:

    hours = [time.strptime(i[:-1].zfill(4), "%H%M") for i in a]
    

    By using i[:-1] we remove that pesky trailing dot, and .zfill(4) will add enough 0 characters to the left to make it to 4 digits.

    Demo:

    >>> import time
    >>> a = ['800.', '830.', '900.', '30.']
    >>> [time.strptime(i[:-1].zfill(4), "%H%M") for i in a]
    [time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1), time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=30, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1), time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=9, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1), time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=30, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)]
    

    If they are float values instead, use the format() function on them to give you zero-padded values:

    >>> format(800., '04.0f')
    '0800'
    

    So do this:

    hours = [time.strptime(format(i % 2400, '04.0f'), "%H%M") for i in a]
    

    where % 2400 normalizes your values to the 0. to 2399. range.

    0 讨论(0)
  • 2021-01-20 12:25

    You can extract hours, minutes without strptime() in this case:

    >>> from datetime import time
    >>> a = [800., 830., 900., 930., 1000., 1030., 30., 2400.]
    >>> [time(*divmod(int(f) % 2400, 100)) for f in a]
    [datetime.time(8, 0), 
     datetime.time(8, 30), 
     datetime.time(9, 0), 
     datetime.time(9, 30),
     datetime.time(10, 0),
     datetime.time(10, 30),
     datetime.time(0, 30),
     datetime.time(0, 0)]
    

    If you want to use strptime() for whatever reason; you could concisely get the required format using x % y:

    >>> ["%04.0f" % (f % 2400) for f in a]
    ['0800', '0830', '0900', '0930', '1000', '1030', '0030', '0000']
    
    0 讨论(0)
提交回复
热议问题