将datefinder的输出放入列表 [英] making output of datefinder into list

查看:73
本文介绍了将datefinder的输出放入列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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来查找字符串中的日期.例如,从s抓取January 4th, 2017 at 8:00pm并将其转换为2017-01-04 20:00:00.现在,我只想获取输出print(m)并将其转换为包含与相同格式的列表mm.我用

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

但我希望输出为

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?

推荐答案

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

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

这篇关于将datefinder的输出放入列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆