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
485 views
in Technique[技术] by (71.8m points)

python - Convert "unknown format" strings to datetime objects?

This is probably a very basic question but after reading documentation I still can't figure out how to do it...

I have two strings in Python that contain dates of unknown format. I don't know what formats they are in, except I know that both are valid date-time expressions. For example, one of them might be in the ISO format and the other in some other format.

All I need is to be able to compare the dates. What's the correct way to turn strings into appropriate date-time objects so that they can be compared?

thanks!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

The dateutil module has a date parser which can parse date strings in many formats.

For example,

In [13]: import dateutil.parser as parser

In [14]: parser.parse("19970902T090000")
Out[14]: datetime.datetime(1997, 9, 2, 9, 0)

In [15]: import datetime as dt

In [16]: now = dt.datetime.now()

In [17]: now.isoformat()
Out[18]: '2012-11-06T15:08:51.393631'

In [19]: parser.parse('2012-11-06T15:08:51.393631')
Out[19]: datetime.datetime(2012, 11, 6, 15, 8, 51, 393631)

In [20]: parser.parse('November 6, 2012')
Out[20]: datetime.datetime(2012, 11, 6, 0, 0)

Note that some datetime strings can be ambiguous: 10-09-2003 could mean October 9 or September 10, for example. dateutil has parameters like dayfirst and yearfirst to handle this:

In [21]: parser.parse("10-09-2003")
Out[21]: datetime.datetime(2003, 10, 9, 0, 0)

In [22]: parser.parse("10-09-2003", dayfirst = True)
Out[22]: datetime.datetime(2003, 9, 10, 0, 0)

In [23]: parser.parse("10-09-03", yearfirst = True)
Out[23]: datetime.datetime(2010, 9, 3, 0, 0)

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

...