我有一个韩文日期字符串,看起来像这样:
월요일, 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-8
或unicode-escape
进行解码和编码,但这些都不起作用。你知道吗
以上所有代码都使用ko_KR
语言环境在Mac/Linux上运行良好。但是,ko_KR
在Windows上也不起作用。你知道吗
有人知道这是怎么回事吗?不知何故,语言环境和语言支持不能正确地处理外来字符。你知道吗
应用
locale.setlocale(locale.LC_ALL, 'kor')
而不是locale.setlocale(locale.LC_TIME, 'kor')
,如下所示:仅供参考,下面是一些其他测试用例(Python 3.5.1 64位(AMD64)on win32):
也许解决办法是:
将month重命名为number of month
按每月子字符串的出现次数选择
第二个想法是作为类https://gist.github.com/mrbannyjo/f83b1a2ab302b0afee49d976de365aae实现的
相关问题 更多 >
编程相关推荐