Parsing 空格分隔,日志文件中大括号内除外-Python

Parsing 空格分隔,日志文件中大括号内除外-Python,parsing,python-3.x,delimiter,logfile,logfile-analysis,Parsing,Python 3.x,Delimiter,Logfile,Logfile Analysis,我是一个长期的读者,第一次询问(请温柔) 在Unix Bash中阅读时,我一直在用一个相当混乱的程序来完成这项工作,但我正在学习python,并希望尝试制作一个更有效的解析器例程 所以我有一堆日志文件,它们大部分是以空格分隔的,但是在可能有空格的地方包含方括号。在查找分隔符时,如何忽略大括号中的内容 (我假设要做到这一点,需要重新构建库) i、 e.样本输入: [21/Sep/2014:13:51:12 +0000] serverx 192.0.0.1 identity 200 8.8.8.8

我是一个长期的读者,第一次询问(请温柔)

在Unix Bash中阅读时,我一直在用一个相当混乱的程序来完成这项工作,但我正在学习python,并希望尝试制作一个更有效的解析器例程

所以我有一堆日志文件,它们大部分是以空格分隔的,但是在可能有空格的地方包含方括号。在查找分隔符时,如何忽略大括号中的内容

(我假设要做到这一点,需要重新构建库)

i、 e.样本输入:

[21/Sep/2014:13:51:12 +0000] serverx 192.0.0.1 identity 200 8.8.8.8 - 500 unavailable RESULT 546 888 GET http ://www.google.com/something/fsd?=somegibberish&youscanseethereisalotofcharactershere+bananashavealotofpotassium [somestuff/1.0 (OSX v. 1.0; this_is_a_semicolon; colon:93.1.1) Somethingelse/1999 (COMMA, yep_they_didnt leave_me_a_lot_to_make_this_easy) DoesanyonerememberAOL/1.0]
期望输出:

'21/Sep/2014:13:51:12 +0000'; 'serverx'; '192.0.0.1'; 'identity'; '200'; '8.8.8.8'; '-'; '500'; 'unavailable'; 'RESULT'; '546'; '888'; 'GET'; 'htp://www.google.com/something/fsd?=somegibberish&youscanseethereisalotofcharactershere+bananashavealotofpotassium'; 'somestuff/1.0 (OSX v. 1.0; this_is_a_semicolon; rev:93.1.1) Somethingelse/1999 (COMMA, yep_they_didnt leave_me_a_lot_to_make_this_easy DoesanyonerememberAOL/1.0'
如果您注意到第一个和最后一个字段(在方括号中的字段)仍然保留完整的空格

奖励积分 第14个字段(URL)始终采用以下格式之一:

  • htp://google.com/path-data-might-be-here-and-can-contain-special-characters

  • google.com/path-data-maybe-be-here-and-can-contain-special-characters

  • xyz.abc.www.google.com/path-data-maybe-be-here-and-can-contain-special-characters

  • google.com:443

  • 谷歌网站
我想在数据中添加一列,只包括域(即xyz.abc.www.google.com或google.com)

到目前为止,我一直在使用Unix AWK和IF语句获取解析后的输出,以“/”分隔该字段,并检查第三个字段是否为空。如果是,则返回第一个字段(直到:如果存在),否则返回第三个字段)。如果有更好的方法可以做到这一点——最好是在与上面相同的例程中,我很乐意听到它——那么我的最终输出可能是:

'21/Sep/2014:13:51:12 +0000'; 'serverx'; '192.0.0.1'; 'identity'; '200'; '8.8.8.8'; '-'; '500'; 'unavailable'; 'RESULT'; '546'; '888'; 'GET'; 'htp://www.google.com/something/fsd?=somegibberish&youscanseethereisalotofcharactershere+bananashavealotofpotassium'; 'somestuff/1.0 (OSX v. 1.0; this_is_a_semicolon; rev:93.1.1) Somethingelse/1999 (COMMA, yep_they_didnt leave_me_a_lot_to_make_this_easy DoesanyonerememberAOL/1.0'; **'www.google.com'**

脚注:我在示例中将http更改为htp,因此它不会创建一堆分散注意力的链接。

正则表达式模式
\[^\]*\]\]\S+
将标记您的数据,尽管它不会从多字值中去掉括号。您需要在单独的步骤中执行此操作:

import re

def parse_line(line):
    values = re.findall(r'\[[^\]]*\]|\S+', line)
    values = [v.strip("[]") for v in values]
    return values
下面是正则表达式模式的更详细版本:

pattern = r"""(?x)   # turn on verbose mode (ignores whitespace and comments)
    \[       # match a literal open bracket '['
    [^\]]*   # match zero or more characters, as long as they are not ']'
    \]       # match a literal close bracket ']'
        |        # alternation, match either the section above or the section below
    \S+      # match one or more non-space characters
    """

values = re.findall(pattern, line) # findall returns a list with all matches it finds

是的,就是这样。你能带我看一下findall部分吗?“\[^\]*\]\\]\S+”?我是正则表达式的新手,但正在努力学习。