将字符串日期转换为datetime

时间:2018-05-13 18:57:54

标签: python

我正在尝试通过提取日期来清理一些列表数据。

我的列表数据包含以下代码:

for i in item_list[1::2]:
    print(i)

数据样本:

Before Audio Described PerformancesNext Performance: Friday  22 June 2018  6.30 PM
Tickets from £20.00Next Performance: Sunday  27 May 2018  10.00 AM
Tickets from £9.50Next Performance: Tuesday  29 May 2018  7.30 PM
Tickets from £5.00Next Performance: Thursday  31 May 2018  4.00 PM

我需要提取日期时间类型的日期/时间信息。

我试过

for i in periods[1::2]:
    print(i[i.find(':')+2:])

这给了我:

Friday  22 June 2018  6.30 PM
Sunday  27 May 2018  10.00 AM
Tuesday  29 May 2018  7.30 PM
Thursday  31 May 2018  4.00 PM

我已申请:

for i in periods[1::2]:
    datetime.strptime(i[i.find(':')+2:],'%A  %d %b %Y  %I.%M %p')

到目前为止这是有效的,但在其中一个日期出现错误:

'Sunday  14 October 2018  2.30 PM' does not match format '%A  %d %b %Y  %I.%M %p'

为什么没有转换此字符串的任何想法?

0 个答案:

没有答案