[Tutor] "ImportError: _strptime not supported"

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Oct 3 01:32:29 CEST 2012


On 2 October 2012 23:19,  <akleider at sonic.net> wrote:
> 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.

>
> 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.

> 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.")

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


More information about the Tutor mailing list