将 datefinder 输出到列表中
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
以上使用 datefinder
在字符串中查找日期。比如January 4th, 2017 at 8:00pm
是从s
中抓出来的,转换成2017-01-04 20:00:00
。现在我只想获取输出 print(m)
并将其转换为包含与 print(m)
相同格式的列表 mm
。我用
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]
我该如何更改我的代码才能这样做?
在 print(m)
上,将其更改为 print(m.strftime("%Y-%m-%d %H:%M:%S"))
。 strftime
旨在将 datetime
对象转换为字符串。
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)
并将其转换为包含与 print(m)
相同格式的列表 mm
。我用
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]
我该如何更改我的代码才能这样做?
在 print(m)
上,将其更改为 print(m.strftime("%Y-%m-%d %H:%M:%S"))
。 strftime
旨在将 datetime
对象转换为字符串。