【问题标题】:making output of datefinder into list将 datefinder 的输出放入列表
【发布时间】:2019-09-02 16:16:25
【问题描述】:
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]

如何更改我的代码?

【问题讨论】:

    标签: python-3.x list loops date datefinder


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-10
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 2011-04-25
      • 1970-01-01
      • 2023-02-06
      相关资源
      最近更新 更多