Dlaczego datetime.strptime nie działa w tym prostym przykładzie?

Używam strptime do konwersji łańcucha daty na datetime. Zgodnie z linkowaną stroną formatowanie w ten sposób powinno działać:

>>> # Using datetime.strptime()
>>> dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")

Mój kod to:

import datetime
dtDate = datetime.strptime(sDate,"%m/%d/%Y")

Gdzie sDate = "07/27/2012" (na przykład). (Rozumiem, z tej samej strony, że %Y jest "rok z wiekiem jako liczbą dziesiętną.")

Próbowałem umieścić rzeczywistą wartość sDate w kodzie:

dtDate = datetime.strptime("07/27/2012","%m/%d/%Y")
Ale to nie działa. Pojawia się błąd: AttributeError:' module ' object has no atrybut 'strptime' Co robię źle?
Author: Wikis, 2012-08-22

6 answers

Powinieneś używać datetime.datetime.strptime. Zauważ, że bardzo stare wersje Pythona (2.4 i starsze) nie mają datetime.datetime.strptime; użyj time.strptime W takim razie.

 58
Author: ecatmur,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-22 09:45:59

Importujesz moduł datetime, który nie ma funkcji strptime.

Ten moduł mA obiekt datetime z tą metodą:

import datetime
dtDate = datetime.datetime.strptime(sDate, "%m/%d/%Y")

Alternatywnie można zaimportować obiekt datetime z modułu:

from datetime import datetime
dtDate = datetime.strptime(sDate, "%m/%d/%Y")

Zauważ, że metoda strptime została dodana w Pythonie 2.5; jeśli używasz starszej wersji, użyj następującego kodu:

import datetime, time
dtDate = datetime.datetime(*time.strptime(sDate, "%m/%d/%Y")[:6])
 18
Author: Martijn Pieters,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-22 09:48:39

Ponieważ datetime jest modułem. Klasa datetime.datetime.

import datetime
dtDate = datetime.datetime.strptime(sDate,"%m/%d/%Y")
 6
Author: Ignacio Vazquez-Abrams,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-22 09:34:52

Powinieneś użyć strftime statycznej metody z datetime klasy z datetime modułu. Try:

import datetime
dtDate = datetime.datetime.strptime("07/27/2012", "%m/%d/%Y")
 0
Author: Konrad Hałas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-22 09:36:54

Możesz również wykonać następujące czynności, aby zaimportować datetime

from datetime import datetime as dt

dt.strptime(date, '%Y-%m-%d')
 0
Author: Priyanka Marihal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-14 06:54:05

Jeśli w folderze z Twoim projektem utworzyłeś plik o nazwie "datetime.py"

 0
Author: Ihor Ivasiuk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-12 08:10:57