making output of datefinder into list

喜欢而已 提交于 2019-12-11 16:07:13

问题


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

The above works using datefinder to find dates in string. For example, January 4th, 2017 at 8:00pm is grabbed from s and is converted into 2017-01-04 20:00:00. Now I simply want to get the output print(m) and turn it into a list mm which contains the same format as print(m). I use

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)]

But I want the output to be

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

How do I change my code to do so?


回答1:


On print(m), change it to print(m.strftime("%Y-%m-%d %H:%M:%S")). strftime is designed to convert datetime objects into strings.



来源:https://stackoverflow.com/questions/57760093/making-output-of-datefinder-into-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!