日期时间解析错误,格式错误?
Datetime parsing error, wrong format?
我在解析 Python 3.6 中的日期时间字符串时遇到了一些问题。关键代码是:
datetime.datetime.strptime("Jan 08, 2018 07:04 PM UTC", '%b %d, %Y %I:%M %p %Z')
和堆栈跟踪:
File "marquito.py", line 180, in start
test_date = "" if test_date == "" else datetime.datetime.strptime(test_date + " UTC", "%b %d, %Y %I:%M %p %Z")
File "/usr/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data 'Jan 08, 2018 07:04 PM UTC' does not match format '%b %d, %Y %I:%M %p %Z'
代码有什么问题吗?
%b
区域设置相关。您的系统设置为非英语或 C 语言环境,因此月份名称不匹配。
要查看您当前语言环境中支持的月份名称,运行:
>>> import calendar
>>> print([calendar.month_abbr[i].lower() for i in range(13)])
在解析英文月份名称之前,将您的语言环境设置回 C
或英文。您只需为 LC_TIME
类别执行此操作:
import locale
locale.setlocale(locale.LC_TIME, 'C')
例如,在西班牙语言环境中,无法解析您的日期:
>>> import datetime
>>> import calendar
>>> with calendar.different_locale('es_ES'):
... print([calendar.month_abbr[i].lower() for i in range(13)])
... datetime.datetime.strptime("Jan 08, 2018 07:04 PM UTC", '%b %d, %Y %I:%M %p %Z')
...
['', 'ene', 'feb', 'mar', 'abr', 'may', 'jun', 'jul', 'ago', 'sep', 'oct', 'nov', 'dic']
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data 'Jan 08, 2018 07:04 PM UTC' does not match format '%b %d, %Y %I:%M %p %Z'
但在默认 C
语言环境下解析成功:
>>> with calendar.different_locale('C'):
... print([calendar.month_abbr[i].lower() for i in range(13)])
... datetime.datetime.strptime("Jan 08, 2018 07:04 PM UTC", '%b %d, %Y %I:%M %p %Z')
...
['', 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
datetime.datetime(2018, 1, 8, 19, 4)
我使用未记录的内部 calendar.different_locale()
上下文管理器临时更改LC_TIME
语言环境。它在进入上下文时设置所需的语言环境,并在退出时使用上面的 locale.setlocale(locale.LC_TIME, ...)
调用再次恢复旧语言环境。
我在解析 Python 3.6 中的日期时间字符串时遇到了一些问题。关键代码是:
datetime.datetime.strptime("Jan 08, 2018 07:04 PM UTC", '%b %d, %Y %I:%M %p %Z')
和堆栈跟踪:
File "marquito.py", line 180, in start
test_date = "" if test_date == "" else datetime.datetime.strptime(test_date + " UTC", "%b %d, %Y %I:%M %p %Z")
File "/usr/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data 'Jan 08, 2018 07:04 PM UTC' does not match format '%b %d, %Y %I:%M %p %Z'
代码有什么问题吗?
%b
区域设置相关。您的系统设置为非英语或 C 语言环境,因此月份名称不匹配。
要查看您当前语言环境中支持的月份名称,运行:
>>> import calendar
>>> print([calendar.month_abbr[i].lower() for i in range(13)])
在解析英文月份名称之前,将您的语言环境设置回 C
或英文。您只需为 LC_TIME
类别执行此操作:
import locale
locale.setlocale(locale.LC_TIME, 'C')
例如,在西班牙语言环境中,无法解析您的日期:
>>> import datetime
>>> import calendar
>>> with calendar.different_locale('es_ES'):
... print([calendar.month_abbr[i].lower() for i in range(13)])
... datetime.datetime.strptime("Jan 08, 2018 07:04 PM UTC", '%b %d, %Y %I:%M %p %Z')
...
['', 'ene', 'feb', 'mar', 'abr', 'may', 'jun', 'jul', 'ago', 'sep', 'oct', 'nov', 'dic']
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data 'Jan 08, 2018 07:04 PM UTC' does not match format '%b %d, %Y %I:%M %p %Z'
但在默认 C
语言环境下解析成功:
>>> with calendar.different_locale('C'):
... print([calendar.month_abbr[i].lower() for i in range(13)])
... datetime.datetime.strptime("Jan 08, 2018 07:04 PM UTC", '%b %d, %Y %I:%M %p %Z')
...
['', 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
datetime.datetime(2018, 1, 8, 19, 4)
我使用未记录的内部 calendar.different_locale()
上下文管理器临时更改LC_TIME
语言环境。它在进入上下文时设置所需的语言环境,并在退出时使用上面的 locale.setlocale(locale.LC_TIME, ...)
调用再次恢复旧语言环境。