简体   繁体   English

Python:如何在不知道格式的情况下将字符串转换为日期时间?

[英]Python: How can I convert string to datetime without knowing the format?

I have a field that comes in as a string and represents a time.我有一个以字符串形式出现并代表时间的字段。 Sometimes its in 12 hour, sometimes in 24 hour.有时是 12 小时,有时是 24 小时。 Possible values:可能的值:

  1. 8:26 8:26
  2. 08:26am上午 08:26
  3. 13:27 13:27

Is there a function that will convert these to time format by being smart about it?是否有 function 可以通过智能将这些转换为时间格式? Option 1 doesn't have am because its in 24 hour format, while option 2 has a 0 before it and option 3 is obviously in 24 hour format.选项 1 没有 am,因为它是 24 小时格式,而选项 2 前面有一个 0,而选项 3 显然是 24 小时格式。 Is there a function in Python/ a lib that does: Python/lib 中是否有 function:

time = func(str_time)

super short answer: 超短答案:

from dateutil import parser
parser.parse("8:36pm")
>>>datetime.datetime(2015, 6, 26, 20, 36)
parser.parse("18:36")
>>>datetime.datetime(2015, 6, 26, 18, 36)

Dateutil should be available for your python installation; Dateutil应该可用于python安装; no need for something large like pandas 不需要像熊猫这样的大东西

If you want to extract the time from the datetime object: 如果要从datetime对象提取时间:

t = parser.parse("18:36").time()

which will give you a time object (if that's of more help to you). 这将给您一个time对象(如果这对您有更多帮助)。 Or you can extract individual fields: 或者,您可以提取单个字段:

dt = parser.parse("18:36")
hours = dt.hour
minute = dt.minute

there is one such function in pandas 熊猫有一种这样的功能

import pandas as pd
d = pd.to_datetime('<date_string>')

Using regex to cut string into ['year', 'month', 'day', 'hour', 'minutes', 'seconds'] then unpack it and fill into datetime class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0) , this is the fastest way I tested so far.使用正则表达式将字符串切割成['year', 'month', 'day', 'hour', 'minutes', 'seconds']然后解压并填入 datetime class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0) ,这是我目前测试最快的方式。

    import re
    import pandas as pd
    import datetime
    import timeit

    def date2timestamp_anyformat(format_date):
        numbers = ''.join(re.findall(r'\d+', format_date))
        if len(numbers) == 8:
            d = datetime.datetime(int(numbers[:4]), int(numbers[4:6]), int(numbers[6:8]))
        elif len(numbers) == 14:
            d = datetime.datetime(int(numbers[:4]), int(numbers[4:6]), int(numbers[6:8]), int(numbers[8:10]), int(numbers[10:12]), int(numbers[12:14]))
        elif len(numbers) > 14:
            d = datetime.datetime(int(numbers[:4]), int(numbers[4:6]), int(numbers[6:8]), int(numbers[8:10]), int(numbers[10:12]), int(numbers[12:14]), microsecond=1000*int(numbers[14:]))
        else:
            raise AssertionError(f'length not match:{format_date}')
        return d.timestamp()

and speed test:和速度测试:

    print('regex cut:\n',timeit.timeit(lambda: datetime.datetime(*map(int, re.split('-|:|\s', '2022-08-13 12:23:44.234')[:-1])).timestamp(), number=10000))
    print('pandas to_datetime:\n', timeit.timeit(lambda: pd.to_datetime('2022-08-13 12:23:44.234').timestamp(), number=10000))
    print('datetime with known format:\n',timeit.timeit(lambda: datetime.datetime.strptime('2022-08-13 12:23:44.234', '%Y-%m-%d %H:%M:%S.%f').timestamp(), number=10000))
    print('regex get number first:\n',timeit.timeit(lambda: date2timestamp_anyformat('2022-08-13 12:23:44.234'), number=10000))
    print('dateutil parse:\n', timeit.timeit(lambda: parser.parse('2022-08-13 12:23:44.234').timestamp(), number=10000))

result:结果:

regex cut:
 0.040550945326685905
pandas to_datetime:
 0.8012433210387826
datetime with known format:
 0.09105705469846725
regex get number first:
 0.04557646345347166
dateutil parse:
 0.6404162347316742

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 中不导入 datetime 的情况下将字符串 datetime 转换为 datetime? - How can convert the string datetime to datetime without import datetime in python? 如何在 pandas python 中将字符串转换为日期时间格式? - How to convert string to datetime format in pandas python? 在python pandas中,如何将此格式化的日期字符串转换为datetime - In python pandas, how can I convert this formatted date string to datetime 您可以在不知道格式的情况下使用datetime.strptime吗? - Can you use datetime.strptime without knowing the format? 如何将不带时区的datetime字符串转换为python中带时区的另一个datetime? - How to convert datetime string without timezone to another datetime with timezone in python? 如何将字符串中的时间转换为日期时间格式,以便可以获取两列之间的差异 - How can I convert time in string into a datetime format so that I can get difference between two columns 如何将字符串转换为日期时间格式 - How to convert a string into datetime format python / django:如何将json字符串转换为html格式? - python/django: How can I convert json string to html format? 如何在python list中将列表转换为字符串格式? - How can I convert a list into string format in python list? 如何在 Python 中将此格式转换为日期时间 - How to convert this format to datetime in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM