【问题标题】:why I am getting multiple date from a string which contains only single date and time? Python为什么我从仅包含单个日期和时间的字符串中获取多个日期? Python
【发布时间】:2021-10-07 08:45:38
【问题描述】:

我在 python 中使用 datefinder 模块,我需要从字符串中提取 DateTime,但我从只包含一个日期和时间的字符串中获取多个日期。

代码:

import datefinder
def date_using_datefinder(date_string):
    matches = datefinder.find_dates(date_string)
    for match in matches:
        print(match)

输入:

test3='''
[26/08/2018 06:58:29.126900]
[26/03/2004 06:58:29.126985][SDAP_CODEC][JET_AT_JUMP][43][HTXXHTXX] DUST_RPOP:QFI:9
[02/06/2003 06:58:29.254621][SDAP_CODEC][JET_AT_JUMP][43][HTXXHTXX] DUST_RPOP:QFI:9
[20/05/2022 06:58:29.124898][SDAP_CODEC][JET_AT_JUMP][43][HTXX] DUST_RPOP:QFI:9
[26/08/2020 06:58:29.136579][ALST][stx][29][ggg] JET_AT_JUMP:TRUX_MSGD_HTXX:13265261686865256:QWERT_DUMPING_TDD:45:DUST_RPOP_CVX:32:AXTP_DI:65576
'''

输出:

2018-08-26 06:58:29.126900
2004-03-26 06:58:29.126985
2003-02-06 06:58:29.254621
2022-05-20 06:58:29.124898
2020-08-26 06:58:29.136579
2045-08-02 00:00:00
2032-08-02 00:00:00

为什么最后两个日期会出现,我猜字符串中没有。

PS:我也试过 DateUtil 模块,但它显示 ParseError。

仅供参考,代码为:

from datetime import datetime
from dateutil import tz
import dateutil.parser as dparser
import warnings
warnings.filterwarnings('ignore')

def date_Using_UtilModule(date_string):
    res = dparser.parse(date_string, fuzzy = True)
    return res

res = date_Using_UtilModule("[26/08/2020 06:58:29.136579][ALST][stx][29][ggg] JET_AT_JUMP:TRUX_MSGD_HTXX:13265261686865256:QWERT_DUMPING_TDD:45:DUST_RPOP_CVX:32:AXTP_DI:65576")
print(res)

output:
ParserError: Unknown string format: [26/08/2020 06:58:29.136579][ALST][stx][29][ggg] JET_AT_JUMP:TRUX_MSGD_HTXX:13265261686865256:QWERT_DUMPING_TDD:45:DUST_RPOP_CVX:32:AXTP_DI:65576

注意:在我的情况下使用正则表达式不起作用,因为我的日志行可以有随机模式以及任何 DateTime 格式,或者我可以说不想使用正则表达式。

【问题讨论】:

  • 您的输入在最后一行有 :45::32:,似乎您正在使用的 datefinder 库正在挑选它们作为年份编号(并给它们一年中的今天?)。如果您对输入行进行一些初始预解析(例如,仅选择第一对方括号的内容),您可能会更轻松。
  • 谢谢,@Blckknght 在这里我提供了一些输入行,但是如果 DateTime 不在方括号中并且它可能在字符串中的任何位置,那么它可能对我们没有帮助。

标签: python-3.x datetime python-dateutil datefinder


【解决方案1】:

让我帮助自己

我已经创建了一个 python 库来完成我的任务,如果需要其他人也可以使用这个库。 尝试涵盖大部分日期时间格式,并将更新更多内容。

是时候使用我们自己的库了

Installation -> pip install MyDateTimeLib==0.1.2
Importing as -> from MyDateTimeLib import myfunction
How to use? --> myfunction.date_find("passing date string")
Returns     --> it return the dictionary containing all the dates from the string else null dict.
check on    --> https://pypi.org/project/MyDateTimeLib/0.1.2/

演示:

data_for_date = '''
[26/08/2018 06:58:29.126900]
[26/03/2004 06:58:29.126985][SDAP_CODEC][JET_AT_JUMP][43][HTXXHTXX] DUST_RPOP:QFI:9
[02/06/2003 06:58:29.254621][SDAP_CODEC][JET_AT_JUMP][43][HTXXHTXX] DUST_RPOP:QFI:9[26/03/2036 06:58:29.126985]
[20/05/2022 06:58:29.124898][SDAP_CODEC][JET_AT_JUMP][43][HTXX] DUST_RPOP:QFI:9
[26/08/2020 06:58:29.136579][ALST][stx][29][ggg] JET_AT_JUMP:TRUX_MSGD_HTXX:13265261686865256:QWERT_DUMPING_TDD:45:DUST_RPOP_CVX:32:AXTP_DI:65576
'''

代码:

from MyDateTimeLib import myfunction
for x in data_for_date.splitlines():
    if len(x)>1:
        dic = myfunction.date_find(x)
        print()
        for k,v in dic.items():
            print(k,v)
            

输出:

Date:0 2018-08-26 06:58:29.126900

Date:0 2004-03-26 06:58:29.126985

Date:0 2003-02-06 06:58:29.254621
Date:1 2036-03-26 06:58:29.126985

Date:0 2022-05-20 06:58:29.124898

Date:0 2020-08-26 06:58:29.136579

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2015-05-11
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    相关资源
    最近更新 更多