剥离时间未转换的数据保留

huangapple go评论53阅读模式
英文:

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 &quot;C:\Users13843\Desktop\VS code\Random Projects\Test Folder\datetest.py&quot;, line 7, in &lt;lambda&gt;
    newlst = sorted(data,key=lambda x:datetime.datetime.strptime(x[&#39;startTime&#39;],&#39;%Y-%m-%dT%H:%M:%S.%f&#39;),reverse=True)
  File &quot;C:\Users13843\Desktop\VS code\Random Projects\Test Folder\datetest.py&quot;, line 7, in main
    newlst = sorted(data,key=lambda x:datetime.datetime.strptime(x[&#39;startTime&#39;],&#39;%Y-%m-%dT%H:%M:%S.%f&#39;),reverse=True)
  File &quot;C:\Users13843\Desktop\VS code\Random Projects\Test Folder\datetest.py&quot;, line 24, in &lt;module&gt;
    main()

The time format in the JSON is:

&quot;startTime&quot;:&quot;2023-03-20T07:26:37.5000623+00:00&quot;

and the line in the code for the new list is:

newlst = sorted(data,key=lambda x:datetime.datetime.strptime(x[&#39;startTime&#39;],&#39;%Y-%m-%dT%H:%M:%S.%f&#39;),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 = [
    {&quot;startTime&quot;: &quot;2023-03-20T07:26:37.5000623+00:00&quot;},
    {&quot;startTime&quot;: &quot;2023-03-19T06:02:12.1231233+00:00&quot;},
]

FMT = &quot;%Y-%m-%d %H:%M:%S.%f&quot;


def a(data: list[dict]) -&gt; list[dict]:
    new = deepcopy(data)
    for d in new:
        d[&quot;startTime&quot;] = datetime.strptime(
            d[&quot;startTime&quot;], &quot;%Y-%m-%dT%H:%M:%S.%f3%z&quot;
        ).strftime(FMT)
    return new


def b(data: list[dict]) -&gt; list[dict]:
    new = deepcopy(data)
    for d in new:
        d[&quot;startTime&quot;] = parser.parse(d[&quot;startTime&quot;], fuzzy=True).strftime(FMT)
    return new


def c(data: list[dict]) -&gt; list[dict]:
    new = deepcopy(data)
    for d in new:
        d[&quot;startTime&quot;] = datetime.strptime(
            d[&quot;startTime&quot;][:26], &quot;%Y-%m-%dT%H:%M:%S.%f&quot;
        ).strftime(FMT)
    return new


def d(data: list[dict]) -&gt; list[dict]:
    new = deepcopy(data)
    for d in new:
        d[&quot;startTime&quot;] = datetime.fromisoformat(d[&quot;startTime&quot;]).strftime(FMT)
    return new


print(sorted(a(data), key=lambda d: d[&quot;startTime&quot;]))
print(sorted(b(data), key=lambda d: d[&quot;startTime&quot;]))
print(sorted(c(data), key=lambda d: d[&quot;startTime&quot;]))
print(sorted(d(data), key=lambda d: d[&quot;startTime&quot;]))

Which yields:

[{&#39;startTime&#39;: &#39;2023-03-19 06:02:12.123123&#39;}, {&#39;startTime&#39;: &#39;2023-03-20 07:26:37.500062&#39;}]
[{&#39;startTime&#39;: &#39;2023-03-19 06:02:12.123123&#39;}, {&#39;startTime&#39;: &#39;2023-03-20 07:26:37.500062&#39;}]
[{&#39;startTime&#39;: &#39;2023-03-19 06:02:12.123123&#39;}, {&#39;startTime&#39;: &#39;2023-03-20 07:26:37.500062&#39;}]
[{&#39;startTime&#39;: &#39;2023-03-19 06:02:12.123123&#39;}, {&#39;startTime&#39;: &#39;2023-03-20 07:26:37.500062&#39;}]

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.

huangapple
  • 本文由 发表于 2023年3月31日 02:15:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定