如何在Python中将此格式转换为datetime

如何在Python中将此格式转换为datetime,python,dataframe,datetime,format,valueerror,Python,Dataframe,Datetime,Format,Valueerror,我的数据格式为2021年2月1日@01:22:01.602,我使用此格式将其转换为日期时间: datetime.datetime.strptime(str(01:22:01.602), '%H:%M:%S.%f') 向我提供错误:ValueError:“对象”与格式“%H:%M:%S.%f”不匹配类似的内容: >>> from datetime import datetime >>> date_string = 'Feb 1, 2021 @ 01:22:01

我的数据格式为2021年2月1日@01:22:01.602,我使用此格式将其转换为日期时间:

datetime.datetime.strptime(str(01:22:01.602), '%H:%M:%S.%f')
向我提供错误:ValueError:“对象”与格式“%H:%M:%S.%f”不匹配类似的内容:

>>> from datetime import datetime
>>> date_string = 'Feb 1, 2021 @ 01:22:01.602'
>>> datetime_obj = datetime.strptime(date_string,'%b %d, %Y @ %H:%M:%S.%f')
>>> datetime_obj
datetime.datetime(2021, 2, 1, 1, 22, 1, 602000)

以下内容将对您有所帮助:

import datetime

date_in_special_format="Feb 1, 2021 @ 01:22:01.602"
merkki=date_in_special_format.index('@')
time_in_special_format=date_in_special_format[merkki+2:len(date_in_special_format)]

clocktime=datetime.datetime.strptime(time_in_special_format, '%H:%M:%S.%f').strftime('%H:%M:%S.%f')

print(clocktime)
在您的示例中,
str(01:22:01.602)
不起作用<另一方面,代码>datetime.datetime.strtime(“01:22:01.602,'%H:%M:%S.%f')工作得非常好。