Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

datetime - How do I strtotime in python?

I'm scraping a a page that includes among other things, date information. So I have a variable named warrant_issued that contains u'11/5/2003' -- I want to store this as a machine readable date. PHP has a handy strtotime function that works fabulously. I was hoping that datetime's strptime would help me but it doesn't seem to be available in my version of datetime -- here is everything in my tab complete on datetime.

In [231]: datetime.
datetime.MAXYEAR           datetime.__hash__          datetime.__sizeof__
datetime.MINYEAR           datetime.__init__          datetime.__str__
datetime.__class__         datetime.__name__          datetime.__subclasshook__
datetime.__delattr__       datetime.__new__           datetime.date
datetime.__dict__          datetime.__package__       datetime.datetime
datetime.__doc__           datetime.__reduce__        datetime.datetime_CAPI
datetime.__file__          datetime.__reduce_ex__     datetime.time
datetime.__format__        datetime.__repr__          datetime.timedelta
datetime.__getattribute__  datetime.__setattr__       datetime.tzinfo

I'm using iPython 2.7.2+

Am I barking up the wrong tree here? What's the best way to turn u'11/5/2003' into a date?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

strptime() is definitely the right approach, it's just a class method for the datetime class (confusingly part of the datetime module).

That is, datetime.datetime.strptime() is what you're looking for (and not datetime.strptime().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...