有没有办法将多个日期格式转换成datetime python [英] Is there a way to convert multiple date formats into datetime python
问题描述
有没有一种简单的方法可以将多个日期格式转换成python中的datetime对象?最终这些将被插入到一个DATE类型的MySQL数据库中。
我可能收到什么类型的数据的例子:
2008年8月2日
04-02-2007
2014年5月
转换我想要这些:
2008-08-02
2007-04-02
2014-05-00
有没有一个简单的方法这个还是我必须硬编码每个场景?
使用第三方 dateutil 库:
>>> from dateutil import parser
>>>> parser.parse(2008年8月2日)
datetime.datetime(2008,8,2,0,0)
>>> parser.parse(04-02-2007)
datetime.datetime(2007,4,2,0,0)
>>> parser.parse(2014年5月)
datetime.datetime(2014,5,6,0,0)
转换为所需格式:
>>> parser.parse(2008年8月2日)strftime('%Y-%m-%d')
'2008-08-02'
>>> parser.parse(04-02-2007)。strftime('%Y-%m-%d')
'2007-04-02'
>>> parser.parse(2014年5月)strftime('%Y-%m-%d')
'2014-05-06'
您可以使用pip安装它:
pip install python-dateutil
另见:
>>> parser.parse(2014年5月)
/ pre>
datetime.datetime(2014,5,6,0,0)
>>> #当你想要的日子是0,那是无效的
...
>>> datetime.datetime(2014,5,0,0,0)
追溯(最近的最后一次呼叫):
文件< stdin>,第1行,< module>
AttributeError:类型对象'datetime.datetime'没有属性'datetime'
>>> #解决方法:
...
>>> from datetime import datetime
>>>> datedefault = datetime.now()。replace(day = 1,hour = 0,minute = 0,second = 0,microsecond = 0)
>>> parser.parse(May 2014,default = datedefault).strftime('%Y-%m-%d')
'2014-05-01'
如果日期字符串中未指定日期,并且没有指定默认值,则将需要当天。由于今天是
06 Aug 2016
,所以在$ $ c的答案的第一部分中将该日期替换为0
$ c>2014年5月。Is there an easy way to convert multiple date formats into a datetime object in python? Eventually these will be inserted into a MySQL database with type DATE.
Examples of what type of data I might be receiving:
August 2, 2008
04-02-2007
May 2014
Converted I would like these to be:
2008-08-02
2007-04-02
2014-05-00
Is there a simple way to do this or do I have to hard code each scenario?
解决方案Use the third party dateutil library:
>>> from dateutil import parser >>> parser.parse("August 2, 2008") datetime.datetime(2008, 8, 2, 0, 0) >>> parser.parse("04-02-2007") datetime.datetime(2007, 4, 2, 0, 0) >>> parser.parse("May 2014") datetime.datetime(2014, 5, 6, 0, 0)
Converting to required format:
>>> parser.parse("August 2, 2008").strftime('%Y-%m-%d') '2008-08-02' >>> parser.parse("04-02-2007").strftime('%Y-%m-%d') '2007-04-02' >>> parser.parse("May 2014").strftime('%Y-%m-%d') '2014-05-06'
You can install it with pip:
pip install python-dateutil
Also look at:
>>> parser.parse("May 2014") datetime.datetime(2014, 5, 6, 0, 0) >>> # As you wanted day to be 0 and that is not valid ... >>> datetime.datetime(2014, 5, 0, 0, 0) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: type object 'datetime.datetime' has no attribute 'datetime' >>> # Workaround: ... >>> from datetime import datetime >>> datedefault = datetime.now().replace(day=1, hour=0, minute=0, second=0, microsecond=0) >>> parser.parse("May 2014",default=datedefault).strftime('%Y-%m-%d') '2014-05-01'
If day is not specified in the date string and also no default is specified, it will take the current day. Since today is
06 Aug 2016
, it replaced the day to0
in the first section of the answer for"May 2014"
.这篇关于有没有办法将多个日期格式转换成datetime python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!