解析日期有多种格式

时间:2020-05-27 12:58:52

标签: python python-3.x imaplib

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函数可让我们自动返回干净日期?

1 个答案:

答案 0 :(得分:1)

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('')