Python日期时间.strtime在Windows上使用韩语区域设置

2024-09-28 19:21:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个韩文日期字符串,看起来像这样:

월요일, 2019년 08월 05일 09:33:39

我正在尝试使用datetime.strptime解析它,方法是将locale设置为kor(在Windows上)。格式为%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-8unicode-escape进行解码和编码,但这些都不起作用。你知道吗

以上所有代码都使用ko_KR语言环境在Mac/Linux上运行良好。但是,ko_KR在Windows上也不起作用。你知道吗

有人知道这是怎么回事吗?不知何故,语言环境和语言支持不能正确地处理外来字符。你知道吗


Tags: 字符串fromimport语言datetimedatetimewindows
2条回答

应用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!)

################################################

也许解决办法是:

  1. 将month重命名为number of month

    u'january'.replace('january', 1)
    
  2. 按每月子字符串的出现次数选择

    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)
    

第二个想法是作为类https://gist.github.com/mrbannyjo/f83b1a2ab302b0afee49d976de365aae实现的

相关问题 更多 >