0
import datefinder
import pandas as pd
s = "entries are due by January 4th, 2017 at 8:00pm. created 01/15/2005 by ACME Inc. and associates. here Dec. 24th, 1981 at 6pm."
match = datefinder.find_dates(s)
for m in match:
    print(m)

2017-01-04 20:00:00
2005-01-15 00:00:00
1981-12-24 18:00:00

以上datefinder用于在字符串中查找日期。例如,January 4th, 2017 at 8:00pm被抓取s并转换为2017-01-04 20:00:00. 现在我只想获取输出print(m)并将其转换为mm包含与print(m). 我用

mm = []
for m in match:
    d = pd.Series(m)
    mm.append(m)

mm
[datetime.datetime(2017, 1, 4, 20, 0),
 datetime.datetime(2005, 1, 15, 0, 0),
 datetime.datetime(1981, 12, 24, 18, 0)]

但我希望输出是

mm 
    [2017-01-04 20:00:00,
    2005-01-15 00:00:00,
    1981-12-24 18:00:00]

我如何更改我的代码来做到这一点?

4

1 回答 1

0

打开print(m),将其更改为print(m.strftime("%Y-%m-%d %H:%M:%S"))strftime旨在将datetime对象转换为字符串。

于 2019-09-02T16:33:00.873 回答