简体   繁体   English

如何正确使用 Arrow Python 中的“MMM”解析令牌

[英]How to correctly use the "MMM" parse token in Arrow Python

I've got a number of datetimes in a spreadsheet that look like this:我在电子表格中有许多日期时间,如下所示:

July 29, 2022 @ 9:44 AM
Aug 2, 2022 @ 6:30 PM
...

I'm attempting to parse these with the following but I get an exception:我正在尝试使用以下内容解析这些,但出现异常:

>>> import arrow
>>> myformat = "MMM D, YYYY @ H:MM A"
>>> arrow.get("Aug 2, 2022 @ 11:37 PM", myformat)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\enricojr\Projects\python-lox\.venv\lib\site-packages\arrow\api.py", line 91, in get
    return _factory.get(*args, **kwargs)
  File "C:\Users\enricojr\Projects\python-lox\.venv\lib\site-packages\arrow\factory.py", line 295, in get
    dt = parser.DateTimeParser(locale).parse(
  File "C:\Users\enricojr\Projects\python-lox\.venv\lib\site-packages\arrow\parser.py", line 346, in parse
    return self._build_datetime(parts)
  File "C:\Users\enricojr\Projects\python-lox\.venv\lib\site-packages\arrow\parser.py", line 701, in _build_datetime
    datetime(
ValueError: month must be in 1..12
>>> arrow.__version__
'1.2.2'
>>>

Documentation states that "MMM" is the token for abbreviated months, but it's expecting a number from 1 - 12 instead.文档指出“MMM”是缩写月份的标记,但它期望的是 1 到 12 之间的数字。 Am I using the tokens incorrectly, or is something else wrong here?我是在错误地使用令牌,还是这里有其他问题?

Python version: 3.10.4 Arrow version: 1.2.2 Operating System: Windows 10, and locale is reported by Python as en_US . Python 版本:3.10.4 箭头版本:1.2.2 操作系统:Windows 10,语言环境由 ZA7F5F35426B927411FC9231B56382173 报告为en_US 56382173。

>>> import ctypes
>>> windll = ctypes.windll.kernel32
>>> windll.GetUserDefaultUILanguage()
1033

(1033 is en_US) (1033 是 en_US)

I think the problem is your minutes in the format string:我认为问题出在格式字符串中的分钟数:

myformat = "MMM D, YYYY @ H:MM A"

The minutes should should be mm , lowercase, the problem is there, not with MMM .分钟应该是mm ,小写,问题就在那里,而不是MMM

So when you do arrow.get("Aug 2, 2022 @ 11:37 PM", myformat) , it's complaining that 37 is outside of 1 and 12.因此,当您执行arrow.get("Aug 2, 2022 @ 11:37 PM", myformat)时,它会抱怨 37 在 1 和 12 之外。

https://arrow.readthedocs.io/en/latest/index.html https://arrow.readthedocs.io/en/latest/index.html

  • Month token: MM 01, 02, 03... 11, 12月代币:MM 01、02、03... 11、12

  • Minute token: mm 00, 01, 02 … 58, 59分钟标记:mm 00, 01, 02 ... 58, 59

Also, going forward, beware 'July' and 'Aug' are different formats.此外,展望未来,请注意“七月”和“八月”是不同的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM