imaplib返回的日期采用以下格式:
dates = [
'Mon, 27 May 2019 13:13:02 -0300 (ART)',
'Tue, 28 May 2019 00:28:31 +0800 (CST)',
'Mon, 27 May 2019 18:32:13 +0200',
'Mon, 27 May 2019 18:43:13 +0200',
'Mon, 27 May 2019 19:00:11 +0200',
'27 May 2019 18:54:58 +0100',
'27 May 2019 18:56:02 +0100',
'Mon, 03 Jun 2019 10:19:56 GMT',
'4 Jun 2019 07:46:30 +0100',
'Mon, 03 Jun 2019 18:48:01 +0200',
'5 Jun 2019 10:39:19 +0100'
]
如何将其转换为BST日期时间?
这是到目前为止我尝试过的:
def date_parse(date):
try:
return datetime.strptime(date, '%a, %d %b %Y %H:%M:%S %z')
except ValueError:
try:
return datetime.strptime(date[:-6], '%a, %d %b %Y %H:%M:%S %z')
except ValueError:
try:
return datetime.strptime(date[:-6], '%d %b %Y %H:%M:%S')
except ValueError:
return datetime.strptime(date[:-4], '%a, %d %b %Y %H:%M:%S')
for date in dates:
print(date)
parsed_date = date_parse(date)
print(parsed_date, type(parsed_date))
print('')
但是我得到重复的日期,然后出现Traceback (most recent call last):
错误。
清理这些日期的最佳方法是什么?是否有imaplib
/ email
函数可让我们自动返回清除日期?
parse
中的[dateutil.parser
函数完成了技巧:
from dateutil.parser import parse
dates = [
'Mon, 27 May 2019 13:13:02 -0300 (ART)',
'Tue, 28 May 2019 00:28:31 +0800 (CST)',
'Mon, 27 May 2019 18:32:13 +0200',
'Mon, 27 May 2019 18:43:13 +0200',
'Mon, 27 May 2019 19:00:11 +0200',
'27 May 2019 18:54:58 +0100',
'27 May 2019 18:56:02 +0100',
'Mon, 03 Jun 2019 10:19:56 GMT',
'4 Jun 2019 07:46:30 +0100',
'Mon, 03 Jun 2019 18:48:01 +0200',
'5 Jun 2019 10:39:19 +0100'
]
for date in dates:
print(date, type(date))
print(parse(date), type(parse(date)))
print('')