英文:
Striptime unconverted data remains
问题
我试图从一个JSON文件中获取的信息创建一个新列表,但我遇到了这个问题:
Exception has occurred: ValueError
unconverted data remains: 9+01:00
File "C:\Users13843\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:\Users13843\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:\Users13843\Desktop\VS code\Random Projects\Test Folder\datetest.py", line 24, in <module>
main()
JSON中的时间格式为:
"startTime":"2023-03-20T07:26:37.5000623+00:00"
代码中用于新列表的行是:
newlst = sorted(data,key=lambda x:datetime.datetime.strptime(x['startTime'],'%Y-%m-%dT%H:%M:%S.%f'),reverse=True)
我期望的输出格式为:
2023-03-30 19:25:13.822945
英文:
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:\Users13843\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:\Users13843\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:\Users13843\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
答案1
得分: 0
# a
第一个选项在格式字符串中添加了一个随机的“3”,这是不好的。基本上,它必须匹配第7毫秒位置的任何值,所以平均情况下会失败10次中的9次。
# b
在选项 b 中,正在使用[dateutil.parser](https://dateutil.readthedocs.io/en/stable/parser.html#dateutil.parser.parse)中的模糊特性解析字符串。
# c
最后,如果不需要精度,可以简单地截断前26位数字并使用更简单的格式字符串。
# d
正如Mark在上面的评论中指出的,`fromisoformat` 将在Python 3.11+中起作用。
---
注:这些函数都使用了`deepcopy`,这可能不是高效的做法,我不了解您的具体用例。如果您不需要保留旧列表和新列表,最好直接在原地更新。
另外,我不确定是否建议使用静态格式字符串像这样。通常最好使用`date.isoformat()`或基于地区设置的格式化`%c`。
英文:
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:
from copy import deepcopy
from datetime import datetime
from dateutil import parser
data = [
{"startTime": "2023-03-20T07:26:37.5000623+00:00"},
{"startTime": "2023-03-19T06:02:12.1231233+00:00"},
]
FMT = "%Y-%m-%d %H:%M:%S.%f"
def a(data: list[dict]) -> list[dict]:
new = deepcopy(data)
for d in new:
d["startTime"] = datetime.strptime(
d["startTime"], "%Y-%m-%dT%H:%M:%S.%f3%z"
).strftime(FMT)
return new
def b(data: list[dict]) -> list[dict]:
new = deepcopy(data)
for d in new:
d["startTime"] = parser.parse(d["startTime"], fuzzy=True).strftime(FMT)
return new
def c(data: list[dict]) -> list[dict]:
new = deepcopy(data)
for d in new:
d["startTime"] = datetime.strptime(
d["startTime"][:26], "%Y-%m-%dT%H:%M:%S.%f"
).strftime(FMT)
return new
def d(data: list[dict]) -> list[dict]:
new = deepcopy(data)
for d in new:
d["startTime"] = datetime.fromisoformat(d["startTime"]).strftime(FMT)
return new
print(sorted(a(data), key=lambda d: d["startTime"]))
print(sorted(b(data), key=lambda d: d["startTime"]))
print(sorted(c(data), key=lambda d: d["startTime"]))
print(sorted(d(data), key=lambda d: d["startTime"]))
Which yields:
[{'startTime': '2023-03-19 06:02:12.123123'}, {'startTime': '2023-03-20 07:26:37.500062'}]
[{'startTime': '2023-03-19 06:02:12.123123'}, {'startTime': '2023-03-20 07:26:37.500062'}]
[{'startTime': '2023-03-19 06:02:12.123123'}, {'startTime': '2023-03-20 07:26:37.500062'}]
[{'startTime': '2023-03-19 06:02:12.123123'}, {'startTime': '2023-03-20 07:26:37.500062'}]
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+.
note: These functions all use deepcopy
which is probably not efficient and I don't know your use case. You should just update in place, if you don't need both the old and new lists.
Also, I'm not sure I recommend using a static format string like this. Usually it is optimal to stick with date.isoformat()
or formatting based on locale with %c
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论