简体   繁体   English

如何在 Python 中将此格式转换为日期时间

[英]How to convert this format to datetime in Python

My data is in this format Feb 1, 2021 @ 01:22:01.602, I am using this to convert it to datetime:我的数据采用这种格式 2021 年 2 月 1 日 @ 01:22:01.602,我正在使用它来将其转换为日期时间:

datetime.datetime.strptime(str(01:22:01.602), '%H:%M:%S.%f')

Gives me the error: ValueError:'object' did not match format '%H:%M:%S.%f'给我错误:ValueError:'object'不匹配格式'%H:%M:%S.%f'

Something like this:像这样的东西:

>>> 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)

The following would help you:以下内容将对您有所帮助:

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)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM