Apr-07-2020, 07:14 AM
I'm trying to parse a string as a datetime object and running into issues parsing the timezone offset part.
This is my code:
This is my code:
from datetime import datetime upd_time_str = 'as on : 07 April 2020, 09:00 GMT+5:30' upd_time_str = upd_time_str[upd_time_str.find(':')+2:] print(upd_time_str) upd_time = datetime.strptime(upd_time_str,'%d %B %Y, %H:%M GMT%z') print(upd_time)And here's the output:
07 April 2020, 09:00 GMT+5:30 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) in 3 upd_time_str = upd_time_str[upd_time_str.find(':')+2:] 4 print(upd_time_str) ----> 5 upd_time = datetime.strptime(upd_time_str,'%d %B %Y, %H:%M GMT%z') 6 print(upd_time) ~\AppData\Local\Programs\Python\Python37\lib\_strptime.py in _strptime_datetime(cls, data_string, format) 575 """Return a class cls instance based on the input string and the 576 format string.""" --> 577 tt, fraction, gmtoff_fraction = _strptime(data_string, format) 578 tzname, gmtoff = tt[-2:] 579 args = tt[:6] + (fraction,) ~\AppData\Local\Programs\Python\Python37\lib\_strptime.py in _strptime(data_string, format) 357 if not found: 358 raise ValueError("time data %r does not match format %r" % --> 359 (data_string, format)) 360 if len(data_string) != found.end(): 361 raise ValueError("unconverted data remains: %s" % ValueError: time data '07 April 2020, 09:00 GMT+5:30' does not match format '%d %B %Y, %H:%M GMT%z'EDIT: Interestingly, if I remove the part 'GMT+5:30' (and also remove the relevant format specifier), the code works perfectly fine, so it's the offset portion that seems to be messing things up. FYI I'm on Python 3.7.6 so the ':' in the offset should be parsed properly.