Python: Parse timestamp string with 7 digits for microseconds to datetime

混江龙づ霸主 提交于 2021-02-11 07:18:05

问题


I have a timestamp string that looks like this:

2019-02-16T10:41:20.6080000+01:00

I have to parse it to datetime. Because there are 7 instead of 6 digits for microseconds the following format does not match:

timestamp = "2019-03-14T14:37:37.000000+01:00"
parsed_timestamp = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f%z") #ValueError: time data '2019-03-14T14:37:37.0000000+01:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

How can I parse this format?


回答1:


Using dparser:

import dateutil.parser as dparser
dt_1 = '2019-02-16T10:41:20.6080000+01:00'
print("Datetime: {}".format(dparser.parse(dt_1,fuzzy=True)))

OUTPUT:

Datetime: 2019-02-16 10:41:20.608000+01:00

If you want the date component:

print("Date: {}".format(dparser.parse(dt_1,fuzzy=True).date()))

OUTPUT:

Date: 2019-02-16



回答2:


Looks like you can use simple string slicing.

Ex:

import datetime
timestamp = "2019-02-16T10:41:20.6080000+01:00"
parsed_timestamp = datetime.datetime.strptime(timestamp[:26], "%Y-%m-%dT%H:%M:%S.%f").date() 

print(parsed_timestamp)

Output:

2019-02-16



回答3:


There are actually two things wrong with your data: you have seven digits for microseconds, and your timezone has a colon.

I would use a regex to solve this problem:

timestamp = "2019-02-16T10:41:20.6080000+01:00"
cleaned_timestamp = re.sub('(\d{6})\d(\+\d{2})(:)(\d{2})', r'\1\2\4', timestamp)
parsed_timestamp = datetime.datetime.strptime(cleaned_timestamp, "%Y-%m-%dT%H:%M:%S.%f%z")
parsed_timestamp

Output:

datetime.datetime(2019, 2, 16, 10, 41, 20, 608000, tzinfo=datetime.timezone(datetime.timedelta(0, 3600)))


来源:https://stackoverflow.com/questions/55586325/python-parse-timestamp-string-with-7-digits-for-microseconds-to-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!