【问题标题】:Extract datetime strings with datefinder使用 datefinder 提取日期时间字符串
【发布时间】:2019-09-26 08:33:11
【问题描述】:
matches = list(datefinder.find_dates(str(laslogsystime[0])))
if len(matches) > 0:
    date = matches[0]
    # print(date)

logging = re.findall(r'(.*?)', date)[0]
print(logging)

TypeError:预期的字符串或类似字节的对象

【问题讨论】:

  • laslogsystime[0] 是什么?为什么使用re.findall(r'(.*?)', date)?你想达到什么目的?
  • 您能确定datestr 还是bytes?我认为date 看起来像list 之类的
  • laslogsystime[0] 是一个包含日期的列表

标签: python regex datefinder


【解决方案1】:

您必须尝试提取datefinder 标识为日期时间字符串的字符串

您不需要re.findall,您需要告诉find_dates 通过传递source=True 参数返回包含日期时间和源字符串的元组:

import datefinder
from datetime import datetime

laslogsystime = "August 1990 some text May 21 2015"
matches = [src for time, src in datefinder.find_dates(laslogsystime, source=True)]
print(matches) # => ['August 1990', 'May 21 2015']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-14
    • 2011-03-31
    • 1970-01-01
    • 2023-01-04
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多