简体   繁体   中英

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

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

(1033 is 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 .

So when you do arrow.get("Aug 2, 2022 @ 11:37 PM", myformat) , it's complaining that 37 is outside of 1 and 12.

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

  • Month token: MM 01, 02, 03... 11, 12

  • Minute token: mm 00, 01, 02 … 58, 59

Also, going forward, beware 'July' and 'Aug' are different formats.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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