I am trying to make a new list from information that I’m getting from a JSON file but I’m getting this:
Exception has occurred: ValueError
unconverted data remains: 9+01:00
File "C:\Users\2913843\Desktop\VS code\Random Projects\Test Folder\datetest.py", line 7, in <lambda>
newlst = sorted(data,key=lambda x:datetime.datetime.strptime(x['startTime'],'%Y-%m-%dT%H:%M:%S.%f'),reverse=True)
File "C:\Users\2913843\Desktop\VS code\Random Projects\Test Folder\datetest.py", line 7, in main
newlst = sorted(data,key=lambda x:datetime.datetime.strptime(x['startTime'],'%Y-%m-%dT%H:%M:%S.%f'),reverse=True)
File "C:\Users\2913843\Desktop\VS code\Random Projects\Test Folder\datetest.py", line 24, in <module>
main()
The time format in the JSON is:
"startTime":"2023-03-20T07:26:37.5000623+00:00"
and the line in the code for the new list is:
newlst = sorted(data,key=lambda x:datetime.datetime.strptime(x['startTime'],'%Y-%m-%dT%H:%M:%S.%f'),reverse=True)
My desired output would be in this format:
2023-03-30 19:25:13.822945
>Solution :
If you look in the table of format codes, you will see that %f is:
Microsecond as a decimal number, zero-padded to 6 digits.
So you are trying to format a string with a format specification that doesn’t handle all the characters.
You have a few options. Here are 3:
import datetime
from dateutil import parser
data = [{"startTime": "2023-03-20T07:26:37.5000623+00:00"}]
a = sorted(
data,
key=lambda x: datetime.datetime.strptime(x["startTime"], "%Y-%m-%dT%H:%M:%S.%f3%z"),
reverse=True,
)
b = sorted(
data,
key=lambda x: "{}".format(parser.parse(x["startTime"], fuzzy=True)),
reverse=True,
)
c = sorted(
data,
key=lambda x: datetime.datetime.strptime(
x["startTime"][:26], "%Y-%m-%dT%H:%M:%S.%f"
),
reverse=True,
)
print(a)
print(b)
print(c)
Which yields:
[{'startTime': '2023-03-20T07:26:37.5000623+00:00'}]
[{'startTime': '2023-03-20T07:26:37.5000623+00:00'}]
[{'startTime': '2023-03-20T07:26:37.5000623+00:00'}]
a
The first option adds, a random "3" into the format string, which is no good. Basically, it has to match whatever the 7th millisecond position is, so it will fail 9 of 10 times on average.
b
In option b, the string is being parsed using a fuzzy feature in dateutil.parser.
c
Lastly, if the precision isn’t required, you can just truncate the first 26 digits and use a more simple format string.
d
As Mark noted in the comments above fromisoformat will work in Python 3.11+.
d = sorted(
data,
key=lambda x: datetime.datetime.fromisoformat(x["startTime"]),
reverse=True,
)