Python datetime.strptime 在 Windows 上使用韩语区域
Python datetime.strptime with Korean locale on Windows
我有一个韩文日期字符串,如下所示:
월요일, 2019년 08월 05일 09:33:39
我试图通过将语言环境设置为 kor
(在 Windows 上)使用 datetime.strptime
解析它。格式为 %A, %Y년 %m월 %d일 %H:%M:%S
.
import locale
from datetime import datetime
locale.setlocale(locale.LC_TIME, 'kor')
date_string = '월요일, 2019년 08월 05일 09:33:39'
fromat = '%A, %Y년 %m월 %d일 %H:%M:%S'
time = datetime.strptime(date_string, format)
print(time)
这在格式字符串略有不同的其他语言(例如英语、德语、法语)中效果很好 - 显然。但是,上面的代码引发了 ValueError
:
ValueError: time data '월요일, 2019년 08월 05일 09:33:39' does not match format '%A, %Y년 %m월 %d일 %H:%M:%S'
我还尝试使用 datetime.strftime
:
生成日期字符串
import locale
from datetime import datetime
locale.setlocale(locale.LC_TIME, 'kor')
print(datetime.now().strftime('%A'))
# Prints '¿ù¿äÀÏ'
而 ¿ù¿äÀÏ
与 월요일
(星期一)的工作日不匹配。
我也试过用 UTF-8
或 unicode-escape
解码和编码,但都不起作用。
以上所有代码在使用 ko_KR
语言环境的 Mac/Linux 上运行良好。但是,ko_KR
也不适用于 Windows。
有人知道这里发生了什么吗?不知何故,语言环境和语言支持无法正确处理外来字符。
应用locale.setlocale(locale.LC_ALL, 'kor')
代替locale.setlocale(locale.LC_TIME, 'kor')
如下:
d:\bat> python -q
>>>
>>> import locale
>>> from datetime import datetime
>>>
>>> ### generate a date string with datetime.strftime
...
>>> locale.setlocale(locale.LC_ALL, 'kor') ### crucial point ###
'Korean_Korea.949'
>>> locale.getlocale(locale.LC_TIME)
('Korean_Korea', '949')
>>> print(datetime.now().strftime('%A')) # Prints 월요일 (right!)
월요일
>>>
>>> ### parsing korean date string
...
>>> date_string = '월요일, 2019년 08월 05일 09:33:39'
>>> fromat = '%A, %Y년 %m월 %d일 %H:%M:%S'
>>>
>>> time = datetime.strptime(date_string, fromat)
>>> print(time)
2019-08-05 09:33:39
>>>
仅供参考,这里有一些其他测试用例(Python 3.5.1 64 位 (AMD64) on win32):
import locale
from datetime import datetime
locale.getdefaultlocale() # Echoes ('cs_CZ', 'cp65001')
locale.getlocale(locale.LC_TIME) # Echoes (None, None)
print(datetime.now().strftime('%A')) # Prints Monday (wrong?)
# user’s default setting for LC_TIME category
locale.setlocale(locale.LC_TIME, '') # Echoes 'Czech_Czechia.utf8'
locale.getlocale(locale.LC_TIME) # Echoes ('Czech_Czechia', 'utf8')
print(datetime.now().strftime('%A')) # Prints pondÄÃ (wrong!)
# user’s default setting for all categories
locale.setlocale(locale.LC_ALL, '') # Echoes 'Czech_Czechia.utf8'
locale.getlocale(locale.LC_TIME) # Echoes ('Czech_Czechia', 'utf8')
print(datetime.now().strftime('%A')) # Prints pondělí (right!)
################################################
locale.setlocale(locale.LC_TIME, 'kor')
locale.getlocale(locale.LC_TIME)
print(datetime.now().strftime('%A')) # Prints ¿ù¿äÀÏ (wrong!)
################################################
也许解决方案是:
将月份重命名为月份数
u'january'.replace('january', 1)
select 按子串每月出现次数
ddrs={1:u'янв', 2:u'фев' , 3:u'мар' , 4:u'апр' , 5:u'ма' , 6:u'июн' , 7:u'июл',8:u'авг' , 9:u'сент' , 10:u'окт' , 11:u'ноя' , 12:u'дек'}
numMonth = next((x for x in ddrs if 'январь'.find(ddrs[x])>-1), None)
第二个想法实现为classhttps://gist.github.com/mrbannyjo/f83b1a2ab302b0afee49d976de365aae
我有一个韩文日期字符串,如下所示:
월요일, 2019년 08월 05일 09:33:39
我试图通过将语言环境设置为 kor
(在 Windows 上)使用 datetime.strptime
解析它。格式为 %A, %Y년 %m월 %d일 %H:%M:%S
.
import locale
from datetime import datetime
locale.setlocale(locale.LC_TIME, 'kor')
date_string = '월요일, 2019년 08월 05일 09:33:39'
fromat = '%A, %Y년 %m월 %d일 %H:%M:%S'
time = datetime.strptime(date_string, format)
print(time)
这在格式字符串略有不同的其他语言(例如英语、德语、法语)中效果很好 - 显然。但是,上面的代码引发了 ValueError
:
ValueError: time data '월요일, 2019년 08월 05일 09:33:39' does not match format '%A, %Y년 %m월 %d일 %H:%M:%S'
我还尝试使用 datetime.strftime
:
import locale
from datetime import datetime
locale.setlocale(locale.LC_TIME, 'kor')
print(datetime.now().strftime('%A'))
# Prints '¿ù¿äÀÏ'
而 ¿ù¿äÀÏ
与 월요일
(星期一)的工作日不匹配。
我也试过用 UTF-8
或 unicode-escape
解码和编码,但都不起作用。
以上所有代码在使用 ko_KR
语言环境的 Mac/Linux 上运行良好。但是,ko_KR
也不适用于 Windows。
有人知道这里发生了什么吗?不知何故,语言环境和语言支持无法正确处理外来字符。
应用locale.setlocale(locale.LC_ALL, 'kor')
代替locale.setlocale(locale.LC_TIME, 'kor')
如下:
d:\bat> python -q
>>>
>>> import locale
>>> from datetime import datetime
>>>
>>> ### generate a date string with datetime.strftime
...
>>> locale.setlocale(locale.LC_ALL, 'kor') ### crucial point ###
'Korean_Korea.949'
>>> locale.getlocale(locale.LC_TIME)
('Korean_Korea', '949')
>>> print(datetime.now().strftime('%A')) # Prints 월요일 (right!)
월요일
>>>
>>> ### parsing korean date string
...
>>> date_string = '월요일, 2019년 08월 05일 09:33:39'
>>> fromat = '%A, %Y년 %m월 %d일 %H:%M:%S'
>>>
>>> time = datetime.strptime(date_string, fromat)
>>> print(time)
2019-08-05 09:33:39
>>>
仅供参考,这里有一些其他测试用例(Python 3.5.1 64 位 (AMD64) on win32):
import locale
from datetime import datetime
locale.getdefaultlocale() # Echoes ('cs_CZ', 'cp65001')
locale.getlocale(locale.LC_TIME) # Echoes (None, None)
print(datetime.now().strftime('%A')) # Prints Monday (wrong?)
# user’s default setting for LC_TIME category
locale.setlocale(locale.LC_TIME, '') # Echoes 'Czech_Czechia.utf8'
locale.getlocale(locale.LC_TIME) # Echoes ('Czech_Czechia', 'utf8')
print(datetime.now().strftime('%A')) # Prints pondÄÃ (wrong!)
# user’s default setting for all categories
locale.setlocale(locale.LC_ALL, '') # Echoes 'Czech_Czechia.utf8'
locale.getlocale(locale.LC_TIME) # Echoes ('Czech_Czechia', 'utf8')
print(datetime.now().strftime('%A')) # Prints pondělí (right!)
################################################
locale.setlocale(locale.LC_TIME, 'kor')
locale.getlocale(locale.LC_TIME)
print(datetime.now().strftime('%A')) # Prints ¿ù¿äÀÏ (wrong!)
################################################
也许解决方案是:
将月份重命名为月份数
u'january'.replace('january', 1)
select 按子串每月出现次数
ddrs={1:u'янв', 2:u'фев' , 3:u'мар' , 4:u'апр' , 5:u'ма' , 6:u'июн' , 7:u'июл',8:u'авг' , 9:u'сент' , 10:u'окт' , 11:u'ноя' , 12:u'дек'} numMonth = next((x for x in ddrs if 'январь'.find(ddrs[x])>-1), None)
第二个想法实现为classhttps://gist.github.com/mrbannyjo/f83b1a2ab302b0afee49d976de365aae