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

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

Striptime unconverted data remains

问题

  1. 我试图从一个JSON文件中获取的信息创建一个新列表,但我遇到了这个问题:
  2. Exception has occurred: ValueError
  3. unconverted data remains: 9+01:00
  4. File "C:\Users13843\Desktop\VS code\Random Projects\Test Folder\datetest.py", line 7, in <lambda>
  5. newlst = sorted(data,key=lambda x:datetime.datetime.strptime(x['startTime'],'%Y-%m-%dT%H:%M:%S.%f'),reverse=True)
  6. File "C:\Users13843\Desktop\VS code\Random Projects\Test Folder\datetest.py", line 7, in main
  7. newlst = sorted(data,key=lambda x:datetime.datetime.strptime(x['startTime'],'%Y-%m-%dT%H:%M:%S.%f'),reverse=True)
  8. File "C:\Users13843\Desktop\VS code\Random Projects\Test Folder\datetest.py", line 24, in <module>
  9. main()

JSON中的时间格式为:

  1. "startTime":"2023-03-20T07:26:37.5000623+00:00"

代码中用于新列表的行是:

  1. newlst = sorted(data,key=lambda x:datetime.datetime.strptime(x['startTime'],'%Y-%m-%dT%H:%M:%S.%f'),reverse=True)

我期望的输出格式为:

  1. 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:

  1. Exception has occurred: ValueError
  2. unconverted data remains: 9+01:00
  3. File &quot;C:\Users13843\Desktop\VS code\Random Projects\Test Folder\datetest.py&quot;, line 7, in &lt;lambda&gt;
  4. newlst = sorted(data,key=lambda x:datetime.datetime.strptime(x[&#39;startTime&#39;],&#39;%Y-%m-%dT%H:%M:%S.%f&#39;),reverse=True)
  5. File &quot;C:\Users13843\Desktop\VS code\Random Projects\Test Folder\datetest.py&quot;, line 7, in main
  6. newlst = sorted(data,key=lambda x:datetime.datetime.strptime(x[&#39;startTime&#39;],&#39;%Y-%m-%dT%H:%M:%S.%f&#39;),reverse=True)
  7. File &quot;C:\Users13843\Desktop\VS code\Random Projects\Test Folder\datetest.py&quot;, line 24, in &lt;module&gt;
  8. main()

The time format in the JSON is:

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

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

  1. 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:

  1. 2023-03-30 19:25:13.822945

答案1

得分: 0

  1. # a
  2. 第一个选项在格式字符串中添加了一个随机的3”,这是不好的基本上它必须匹配第7毫秒位置的任何值所以平均情况下会失败10次中的9
  3. # b
  4. 在选项 b 正在使用[dateutil.parser](https://dateutil.readthedocs.io/en/stable/parser.html#dateutil.parser.parse)中的模糊特性解析字符串。
  5. # c
  6. 最后如果不需要精度可以简单地截断前26位数字并使用更简单的格式字符串
  7. # d
  8. 正如Mark在上面的评论中指出的`fromisoformat` 将在Python 3.11+中起作用
  9. ---
  10. 这些函数都使用了`deepcopy`这可能不是高效的做法我不了解您的具体用例如果您不需要保留旧列表和新列表最好直接在原地更新
  11. 另外我不确定是否建议使用静态格式字符串像这样通常最好使用`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:

  1. from copy import deepcopy
  2. from datetime import datetime
  3. from dateutil import parser
  4. data = [
  5. {&quot;startTime&quot;: &quot;2023-03-20T07:26:37.5000623+00:00&quot;},
  6. {&quot;startTime&quot;: &quot;2023-03-19T06:02:12.1231233+00:00&quot;},
  7. ]
  8. FMT = &quot;%Y-%m-%d %H:%M:%S.%f&quot;
  9. def a(data: list[dict]) -&gt; list[dict]:
  10. new = deepcopy(data)
  11. for d in new:
  12. d[&quot;startTime&quot;] = datetime.strptime(
  13. d[&quot;startTime&quot;], &quot;%Y-%m-%dT%H:%M:%S.%f3%z&quot;
  14. ).strftime(FMT)
  15. return new
  16. def b(data: list[dict]) -&gt; list[dict]:
  17. new = deepcopy(data)
  18. for d in new:
  19. d[&quot;startTime&quot;] = parser.parse(d[&quot;startTime&quot;], fuzzy=True).strftime(FMT)
  20. return new
  21. def c(data: list[dict]) -&gt; list[dict]:
  22. new = deepcopy(data)
  23. for d in new:
  24. d[&quot;startTime&quot;] = datetime.strptime(
  25. d[&quot;startTime&quot;][:26], &quot;%Y-%m-%dT%H:%M:%S.%f&quot;
  26. ).strftime(FMT)
  27. return new
  28. def d(data: list[dict]) -&gt; list[dict]:
  29. new = deepcopy(data)
  30. for d in new:
  31. d[&quot;startTime&quot;] = datetime.fromisoformat(d[&quot;startTime&quot;]).strftime(FMT)
  32. return new
  33. print(sorted(a(data), key=lambda d: d[&quot;startTime&quot;]))
  34. print(sorted(b(data), key=lambda d: d[&quot;startTime&quot;]))
  35. print(sorted(c(data), key=lambda d: d[&quot;startTime&quot;]))
  36. print(sorted(d(data), key=lambda d: d[&quot;startTime&quot;]))

Which yields:

  1. [{&#39;startTime&#39;: &#39;2023-03-19 06:02:12.123123&#39;}, {&#39;startTime&#39;: &#39;2023-03-20 07:26:37.500062&#39;}]
  2. [{&#39;startTime&#39;: &#39;2023-03-19 06:02:12.123123&#39;}, {&#39;startTime&#39;: &#39;2023-03-20 07:26:37.500062&#39;}]
  3. [{&#39;startTime&#39;: &#39;2023-03-19 06:02:12.123123&#39;}, {&#39;startTime&#39;: &#39;2023-03-20 07:26:37.500062&#39;}]
  4. [{&#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:

确定