添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
The following code was recently suggested as an example of how the
datetime module could be used to solve a problem. Not having access to
Python at work, I found
http://pythontutor.com/visualize.html
thinking it would allow me to "play with Python" when I have a free moment.

from datetime import datetime
start_date = datetime(year=2012, month=11, day=3)
print(start_date)

datestring = '10/11/2012'
experiment_date = datetime.strftime(datestring, '%d/%m/%Y')
print(experiment_date)

if experiment_date > start_date:
print("Experiment_date comes after start_date.")
else:
print("Expriment_date does not come after start_date.")

Does not complete because of:

"ImportError: _strptime not supported"

At first I thought perhaps strptime was a Python3 feature but both v2.7
and v3.2 result in the same ImportError.
Is the problem with http://pythontutor.com/visualize.html
?

Also, the leading underscore in the error message puzzles me.

According to
http://docs.python.org/library/datetime.html
"""classmethod datetime.strptime(date_string, format)"""
strptime should work.
Forgot to send to the list. Grrr.

---------- Forwarded message ----------
From: Marc Tompkins <***@gmail.com>
Date: Tue, Oct 2, 2012 at 3:32 PM
Subject: Re: [Tutor] "ImportError: _strptime not supported"
Post by a***@sonic.net
The following code was recently suggested as an example of how the
datetime module could be used to solve a problem. Not having access to
Python at work, I found
http://pythontutor.com/visualize.html
thinking it would allow me to "play with Python" when I have a free moment.
from datetime import datetime
start_date = datetime(year=2012, month=11, day=3)
print(start_date)
datestring = '10/11/2012'
experiment_date = datetime.strftime(datestring, '%d/%m/%Y')
print(experiment_date)
print("Experiment_date comes after start_date.")
print("Expriment_date does not come after start_date.")
"ImportError: _strptime not supported"
At first I thought perhaps strptime was a Python3 feature but both v2.7
and v3.2 result in the same ImportError.
Is the problem with http://pythontutor.com/visualize.html
?
Also, the leading underscore in the error message puzzles me.
According to
http://docs.python.org/library/datetime.html
"""classmethod datetime.strptime(date_string, format)"""
strptime should work.
I went to visualize, and pasted in your code; I don't get that ImportError.
Instead, I get
Post by a***@sonic.net
TypeError: descriptor 'strftime' requires a 'datetime.date' object but
received a 'str'
experiment_date = datetime.strftime(datestring, '%d/%m/%Y')
I really have no idea where your ImportError came from, especially since
the code you've posted doesn't explicitly call strptime. I suspect that,
yes, it was a problem on the "visualize" site, and that they've cleared it
up now.
Post by a***@sonic.net
The following code was recently suggested as an example of how the
datetime module could be used to solve a problem. Not having access to
Python at work, I found
http://pythontutor.com/visualize.html
thinking it would allow me to "play with Python" when I have a free moment.
There are a few websites like this. You have to bear in mind that they
usually disable some functionality for security. I would expect the
datetime module to work though.
Post by a***@sonic.net
from datetime import datetime
start_date = datetime(year=2012, month=11, day=3)
print(start_date)
datestring = '10/11/2012'
experiment_date = datetime.strftime(datestring, '%d/%m/%Y')
The example I posted uses strptime not strftime. Note the 'p' in the
middle instead of the 'f'. These two functions are opposites: strptime
turns string objects into datetime objects and strftime does the
inverse.

Yes, it is silly to have functions with such similar names that are
hard to distinguish visually. Unfortunately Python inherited these
names from the C programming language where it was more difficult to
use good names for functions.
Post by a***@sonic.net
print(experiment_date)
print("Experiment_date comes after start_date.")
print("Expriment_date does not come after start_date.")
Otherwise you've got the right idea. Although I think for the original
problem it should be:

if experiment_date >= start_date:

Note the '>=' instead of '>'.


Oscar
Post by a***@sonic.net
from datetime import datetime
start_date = datetime(year=2012, month=11, day=3)
print(start_date)
datestring = '10/11/2012'
experiment_date = datetime.strftime(datestring, '%d/%m/%Y')
print(experiment_date)
print("Experiment_date comes after start_date.")
print("Expriment_date does not come after start_date.")
"ImportError: _strptime not supported"
That error is surely some quirk of pythontutor.com.

datetime.strftime calls time.strftime. On the other hand,
datetime.strptime does import _strptime:

http://hg.python.org/cpython/file/70274d53c1dd/Modules/datetimemodule.c#l3937

Did you actually use datetime.strptime?
Post by a***@sonic.net
Post by a***@sonic.net
from datetime import datetime
datestring = '10/11/2012'
dt = datetime.strptime(datestring, '%d/%m/%Y')
dt
datetime.datetime(2012, 11, 10, 0, 0)
Post by a***@sonic.net
Post by a***@sonic.net
dt.strftime('%d/%m/%Y')
'10/11/2012'