ubuntuusers.de

Python datetime does not match format

Status: Gelöst | Ubuntu-Version: Ubuntu GNOME 16.04 (Xenial Xerus)
Antworten |

Tronde Team-Icon

Avatar von Tronde

Anmeldungsdatum:
23. November 2006

Beiträge: 1640

Hallo,

beim Versuch einen String in ein Datum umzuwandeln stoße ich auf einen Fehler, dessen Ursache mir nicht ersichtlich ist. Evtl. könnt ihr mir aber auf die Sprünge helfen und das Brett von meinem Kopf lösen.

Ich nutze Python 3.5.2 mit dem Modul datetime 🇬🇧:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
Python 3.5.2 (default, Sep 10 2016, 08:21:44) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> import locale
>>> locale.getlocale()
('de_DE', 'UTF-8')
>>> date_object1 = datetime.strptime('22. Oktober 2015 18:15', '%d %B %Y %H:%M')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/usr/lib/python3.5/_strptime.py", line 343, in _strptime
    (data_string, format))
ValueError: time data '22. Oktober 2015 18:15' does not match format '%d %B %Y %H:%M'
>>>

Meiner Meinung nach, sollte die aktuelle Einstellung für locale korrekt sein, um '%B' verwenden zu können. Habe ich hier etwas übersehen? Was mache ich falsch?

Gruß
Tronde

user_unknown

Avatar von user_unknown

Anmeldungsdatum:
10. August 2005

Beiträge: 17622

Wohnort: Berlin

Ohne Python zu können, von ähnlichen Problemen in anderen Sprachen: der Punkt nach der 22 scheint mir verdächtig.

Tronde Team-Icon

(Themenstarter)
Avatar von Tronde

Anmeldungsdatum:
23. November 2006

Beiträge: 1640

Hi,

gute Idee. Ich habe es nun mal wie folgt versucht:

1
2
3
4
5
6
7
8
>>> date_object1 = datetime.strptime('22. Oktober 2015 18:15', '%d. %B %Y %H:%M')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/usr/lib/python3.5/_strptime.py", line 343, in _strptime
    (data_string, format))
ValueError: time data '22. Oktober 2015 18:15' does not match format '%d. %B %Y %H:%M'

Leider noch ohne Erfolg. ☹

Axel-Erfurt

Anmeldungsdatum:
18. Mai 2016

Beiträge: 1347

versuche mal

'%d. %F %Y %H:%M'

Tronde Team-Icon

(Themenstarter)
Avatar von Tronde

Anmeldungsdatum:
23. November 2006

Beiträge: 1640

Hm, '%F' wird in der Dokumentation nicht erwähnt. Es kommt eine entsprechende Fehlermeldung.

1
2
3
4
5
6
7
8
>>> date_object1 = datetime.strptime('22. Oktober 2015 18:15', '%d. %F %Y %H:%M')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/usr/lib/python3.5/_strptime.py", line 335, in _strptime
    (bad_directive, format)) from None
ValueError: 'F' is a bad directive in format '%d. %F %Y %H:%M'

barcc

Avatar von barcc

Anmeldungsdatum:
13. Juli 2007

Beiträge: 696

Wohnort: Dortmund

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Python 3.5.2 (default, Sep 10 2016, 08:21:44) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> import locale
>>> locale.getlocale()
('de_DE', 'UTF-8')
>>> datetime.strptime('22. October 2015 18:15', '%d. %B %Y %H:%M')
datetime.datetime(2015, 10, 22, 18, 15)
>>> 

Anscheinend funktionieren deutsche Monatsnamen einfach nicht...

seahawk1986

Anmeldungsdatum:
27. Oktober 2006

Beiträge: 11265

Wohnort: München

Witzig, mit bpython (bzw. bpython3 unter Ubuntu) klappt es, weil LC_TIME gleich korrekt gesetzt ist, mit dem normalen Python-Interpreter nicht, da muss man es nachholen:

1
2
3
4
5
6
7
8
$ bpython
bpython version 0.16 on top of Python 3.5.2 /usr/bin/python
>>> from datetime import datetime
>>> import locale
>>> datetime.strptime('22. Oktober 2015 18:15', '%d. %B %Y %H:%M')
datetime.datetime(2015, 10, 22, 18, 15)
>>> locale.getlocale(locale.LC_TIME)
('de_DE', 'UTF-8')
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$ python
Python 3.5.2 (default, Jun 28 2016, 08:46:01) 
[GCC 6.1.1 20160602] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> import locale
>>> datetime.strptime('22. Oktober 2015 18:15', '%d. %B %Y %H:%M')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/usr/lib/python3.5/_strptime.py", line 343, in _strptime
    (data_string, format))
ValueError: time data '22. Oktober 2015 18:15' does not match format '%d. %B %Y %H:%M'
>>> locale.getlocale(locale.LC_TIME)
(None, None)
>>> locale.setlocale(locale.LC_TIME,'')
'de_DE.UTF-8'
>>> datetime.strptime('22. Oktober 2015 18:15', '%d. %B %Y %H:%M')
datetime.datetime(2015, 10, 22, 18, 15)

seahawk1986

Anmeldungsdatum:
27. Oktober 2006

Beiträge: 11265

Wohnort: München

Als Erklärung aus der Dokumentation: https://docs.python.org/3.5/library/locale.html#background-details-hints-tips-and-caveats

Initially, when a program is started, the locale is the C locale, no matter what the user’s preferred locale is. There is one exception: the LC_CTYPE category is changed at startup to set the current locale encoding to the user’s preferred locale encoding. The program must explicitly say that it wants the user’s preferred locale settings for other categories by calling

setlocale(LC_ALL, '')

Tronde Team-Icon

(Themenstarter)
Avatar von Tronde

Anmeldungsdatum:
23. November 2006

Beiträge: 1640

Danke für den Hinweis. Damit klappt es jetzt auch mit den deutschen Monatsnamen.

Antworten |