python strptime format with optional bits

后端 未结 7 683
慢半拍i
慢半拍i 2020-12-03 09:51

Right now I have:

timestamp = datetime.strptime(date_string, \'%Y-%m-%d %H:%M:%S.%f\')

This works great unless I\'m converting a string tha

7条回答
  •  囚心锁ツ
    2020-12-03 10:12

    What about just appending it if it doesn't exist?

    if '.' not in date_string:
        date_string = date_string + '.0'
    
    timestamp = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f')
    

提交回复
热议问题