python从字符串提取日期,从Python中的字符串中提取日期时间的最佳方法

I have a script that is parsing out fields within email headers that represent dates and times. Some examples of these strings are as follows:

Fri, 10 Jun 2011 11:04:17 +0200 (CEST)

Tue, 1 Jun 2011 11:04:17 +0200

Wed, 8 Jul 1992 4:23:11 -0200

Wed, 8 Jul 1992 4:23:11 -0200 EST

Before I was confronted with the CEST/EST portions at the ends of some the strings I had things working pretty well just using datetime.datetime.strptime like this:

msg['date'] = 'Wed, 8 Jul 1992 4:23:11 -0200'

mail_date = datetime.datetime.strptime(msg['date'][:-6], '%a, %d %b %Y %H:%M:%S')

I tried to put a regex together to match the date portions of the string while excluding the timezone information at the end, but I was having issues with the regex (I couldn't match a colon).

Is using a regex the best way to parse all of the examples above? If so, could someone share a regex that would match these examples? In the end I am looking to have a datetime object.

解决方案from email import utils

utils.parsedate_tz('Fri, 10 Jun 2011 11:04:17 +0200 (CEST)')

utils.parsedate_tz('Fri, 10 Jun 2011 11:04:17 +0200')

utils.parsedate_tz('Fri, 10 Jun 2011 11:04:17')

The output is:

(2011, 6, 10, 11, 4, 17, 0, 1, -1, 7200)

(2011, 6, 10, 11, 4, 17, 0, 1, -1, 7200)

(2011, 6, 10, 11, 4, 17, 0, 1, -1, None)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值