Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Treat character as literal in strptime

Another application that I have no control over returns datetime values as Jan 1, 2019 09:00:00.000 X-0500. The X preceeding the timezone is driving me bonker since I cannot find out how to ignore it. Without the X, this works:

from datetime import datetime
datetime.strptime('Jan 1, 2019 09:00:00.000 -0500', '%b %d, %Y %H:%M:%S.%f %z')

I can manually remove the X but is there a way to write the format string such that Python treats the X as a literal X with no special meaning?

datetime.strptime('Jan 1, 2019 09:00:00.000 X-0500', '???')
like image 493
Jenny Avatar asked Dec 20 '25 03:12

Jenny


1 Answers

Just write a literal X in the format string:

datetime.strptime('Jan 1, 2019 09:00:00.000 X-0500', '%b %d, %Y %H:%M:%S.%f X%z')
                                                                            ^
like image 107
jwodder Avatar answered Dec 21 '25 15:12

jwodder