Search code examples
python-3.xlistloopsdatedatefinder

making output of datefinder into list


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?


Solution

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