gpt4 book ai didi

python - dateutil/parser 中的语言环境

转载 作者:行者123 更新时间:2023-11-28 16:24:46 25 4
gpt4 key购买 nike

我设置

locale.setlocale(locale.LC_TIME, ('de', 'UTF-8'))

要解析的字符串是:

Montag, 11. April 2016 19:35:57

我使用:

note_date = parser.parse(result.group(2))

但出现以下错误:

Traceback (most recent call last): File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1531, in globals = debugger.run(setup['file'], None, None, is_module) File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 938, in run pydev_imports.execfile(file, globals, locals) # execute the script File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/Users/adieball/Dropbox/Multiverse/Programming/python/repositories/kindle/kindle2en.py", line 250, in main(sys.argv[1:]) File "/Users/adieball/Dropbox/Multiverse/Programming/python/repositories/kindle/kindle2en.py", line 154, in main note_date = parser.parse(result.group(2)) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/dateutil/parser.py", line 1164, in parse return DEFAULTPARSER.parse(timestr, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/dateutil/parser.py", line 555, in parse raise ValueError("Unknown string format") ValueError: Unknown string format

调试显示解析器没有使用“正确的”dateutil 值(德语),它仍在使用英语值。

enter image description here

我确定我在这里遗漏了一些明显的东西,但找不到它。

谢谢。

最佳答案

dateutil.parser 不使用 locale。你需要继承 dateutil.parser.parserinfo并构造一个德语等价物:。

from dateutil import parser

class GermanParserInfo(parser.parserinfo):
WEEKDAYS = [("Mo.", "Montag"),
("Di.", "Dienstag"),
("Mi.", "Mittwoch"),
("Do.", "Donnerstag"),
("Fr.", "Freitag"),
("Sa.", "Samstag"),
("So.", "Sonntag")]

s = 'Montag, 11. April 2016 19:35:57'
note_date = parser.parse(s, parserinfo=GermanParserInfo())

您需要对其进行扩展以使其也适用于其他值,例如月份名称。

关于python - dateutil/parser 中的语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37485174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com