尽管语法正确,但以两种方式解析时间戳会产生异常

时间:2018-11-19 02:44:14

标签: python timestamp pyparsing

我正在尝试解析日志文件中的时间戳,该日志文件可能以两种不同的格式出现。格式#1可以完美工作,而格式#2总是会引发异常。希望这里的聪明人可以发现该错误。

    # testing out log parsing
    import string
    import pyparsing as pp
    from pyparsing import *



    # timestamp and using suppress
    integer = Word(nums)
    month = Word(alphas)
    timeZoneOffset = Word("+-",nums)
    timeZoneOffset1 = Word(alphas)
    log4 = Group( Suppress("[") +
                    Combine( integer + "/" + month + "/" + integer +
                            ":" + integer + ":" + integer + ":" + 
       integer )  + timeZoneOffset  +
                    Suppress("]") )

     datetime = "[20/Jan/2003:08:55:36 -0800]"

     serverdatetime = log4.parseString(datetime)
     print(serverdatetime)



     log5 = Group( Suppress("[") +
                    Combine( month + month + integer + integer +
                            ":" + integer + ":" + integer + integer )  
      + timeZoneOffset1  +
                    Suppress("]") )

     log6 = "[" + Word(alphas) + Word(alphas) + Word(nums) + Word(nums) 
     + ":" + Word(nums) + ":" + Word(nums) + restOfLine + "]"
     datetime1 = "[Thu Nov 01 15:02:52 2018 UTC]"
     serverdatetime1 = log6.parseString(datetime1)
     print(serverdatetime1)

这是我得到的输出:

    python test_message.py 
    [['20/Jan/2003:08:55:36', '-0800']]

    Traceback (most recent call last):
    File "../python3.6/site-packages/pyparsing.py", line 1379, in 
    _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
    File "../python3.6/site-packages/pyparsing.py", line 2410, in 
    parseImpl
    if (instring[loc] == self.firstMatchChar and
    IndexError: string index out of range



    pyparsing.ParseException: Expected "]" (at char 30), (line:1, 
    col:31)

0 个答案:

没有答案