无法将时间字符串从 csv 转换为日期时间

Can't convert time strings from csv to datetime

我有一个 csv 文件,每行有 2 个时间戳,包括日期、时间和一个值:

02.04.2019 00:30:30;02.04.2019 00:36:05;6354
02.04.2019 02:36:05;02.04.2019 03:41:40;3453
02.04.2019 03:41:40;02.04.2019 04:47:15;5456

当我尝试将日期和时间字符串转换为日期时间时:

with open("file.csv", "rt", encoding='utf-8-sig') as source:
    reader = csv.reader(source)
    for row in csv.reader(source, delimiter=";"):
        dateTimeBegin = row[0]
        dateTimeEnd = row[1]
        begin_intervall = datetime.datetime.strptime("%d.%m.%Y %I:%M:%S", dateTimeBegin)
        end_intervall = datetime.datetime.strptime("%d.%m.%Y %I:%M:%S", dateTimeEnd)

我得到了:

Traceback (most recent call last):
  File ".\compare.py", line 48, in <module>
    begin_intervall = datetime.datetime.strptime("%d.%m.%Y %I:%M:%S", dateTimeBegin)
  File "C:\Users\foobar\AppData\Local\Programs\Python\Python38-32\lib\_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "C:\Users\foobar\AppData\Local\Programs\Python\Python38-32\lib\_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '%d.%m.%Y %I:%M:%S' does not match format '02.04.2019 00:30:30'

这是为什么?当我尝试

date_time_str ="08.04.2018 05:35:38"     
date_time_obj = datetime.datetime.strptime(date_time_str, '%d.%m.%Y %I:%M:%S') 

它工作正常。

编辑: 我交换了格式和时间字符串并使用了一些其他数据:

02.04.2019 01:30:30;02.04.2019 02:36:05;6354 #works
02.04.2019 02:36:05;02.04.2019 03:41:40;3453 #works
02.04.2019 03:41:40;02.04.2019 04:47:15;5456 #works
03.04.2019 00:25:03;03.04.2019 01:30:12;5997 #ERROR
03.04.2019 01:30:12;03.04.2019 02:35:21;5993
03.04.2019 02:35:21;03.04.2019 03:40:30;5994

错误仍然是:

Traceback (most recent call last):
  File ".\new.py", line 12, in <module>
    begin_intervall = datetime.datetime.strptime(dateTimeBegin, "%d.%m.%Y %I:%M:%S")
  File "C:\Users\foobar\AppData\Local\Programs\Python\Python38-32\lib\_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "C:\Users\foobar\AppData\Local\Programs\Python\Python38-32\lib\_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '03.04.2019 00:25:03' does not match format '%d.%m.%Y %I:%M:%S'

好像是00:25:03。但是 %I 是 24 小时格式。

你给的参数顺序错误。日期字符串需要先行,然后是格式字符串。

根据日期时间docs

函数定义为datetime.strptime(date_string, format)

这意味着第一个参数将是日期字符串 (02.04.2019 01:30:30) 或变量 dateTimeBegin,第二个参数将是格式字符串 ('%d.%m.%Y %I:%M:%S').

我认为这适用于 02.04.2019 01:30:30 而不是 03.04.2019 00:25:03 的原因是因为对于 %H 和 1 到 12,小时数应该在 0 到 23 之间对于 %I,您可以找到此 here 的 table。这是因为 %H 代表 24 小时格式的小时,而 %I 代表 12 小时格式的小时。

要解决此问题,您应该将格式字符串设置为 '%d.%m.%Y %H:%M:%S' 而不是 '%d.%m.%Y %I:%M:%S'

顺便说一句,如果您是记录这些时间戳的人,因此可以控制它们的记录方式,我建议您使用 Unix Timestamps instead. This is because they are much easier to convert back and forth and leave no ambiguity about the time zone of these timestamps. If you decide to do this however, be sure to localize your datetime objects before you actually save them to the CSV file as unix timestamps. This 问题有很多很好的例子说明如何做到这一点。