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

4

5

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?

Readytowear answered 9/4, 2019 at 6:24 Comment(2)
What is your desired output?Poplin
Do you only want the date from it?Poplin
P
2

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
Poplin answered 9/4, 2019 at 6:31 Comment(0)
K
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
Koon answered 9/4, 2019 at 6:53 Comment(0)
H
1

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)))
Haney answered 9/4, 2019 at 6:31 Comment(0)
A
1

With Python 3.11, fromisoformat handles 7 digits of fractional seconds and parses the Z to UTC:

Python 3.11.3 (main, May  3 2023, 11:09:17) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

from datetime import datetime
dt_a_str = '2022-04-16T14:27:47.1069564Z'

print(datetime.fromisoformat(dt_a_str))
2022-04-16 14:27:47.106956+00:00

If you require naive datetime (no tzinfo), you can remove it by calling .replace(tzinfo=None) on the datetime object.

Antispasmodic answered 8/7 at 14:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.