Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - locale in dateutil / parser

I set

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

the string to parse is:

Montag, 11. April 2016 19:35:57

I use:

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

but get the following error:

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

a debug show that parser is not using the "correct" dateutil values (german), it's still using the english ones.

enter image description here

I'm sure I'm missing something obvious here, but can't find it.

Thanks.

like image 695
f0rd42 Avatar asked May 27 '16 13:05

f0rd42


People also ask

What is dateutil parser in Python?

This module offers a generic date/time string parser which is able to parse most known formats to represent a date and/or time. This module attempts to be forgiving with regards to unlikely input formats, returning a datetime object even for dates which are ambiguous.

How do you parse a date format in Python?

Python has a built-in method to parse dates, strptime . This example takes the string “2020–01–01 14:00” and parses it to a datetime object. The documentation for strptime provides a great overview of all format-string options.

Is dateutil a standard Python library?

The dateutil module specializes in providing an extension of features to the existing datetime module, and as such, the installation of the datetime module is a prerequisite. However, since it's a part of the Python standard library, there's nothing to worry about.


1 Answers

dateutil.parser doesn't use locale. You'll need to subclass dateutil.parser.parserinfo and construct a German equivalent:.

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())

You'd need to extend this to also work for other values, such as month names.

like image 57
Simeon Visser Avatar answered Oct 07 '22 05:10

Simeon Visser