python - 将日志文件解析为其嵌套的开始和结束对的算法/Python

标签 python algorithm data-structures

我正在尝试帮助读取日志文件。

我已经设法转换了日志文件的每一行,这样我就有了一个 Python 字典,其中包含关于每一行的事实,这意味着我将文件作为一个数组保存在内存中,如下所示:

[
    {'keyword':'a', 'is_pair':True, 'type':'open', 'details':'iwiv', 'linenumber':5},
    {'keyword':'a', 'is_pair':True, 'type':'open', 'details':'83fi', 'linenumber':200},
    {'keyword':'a', 'is_pair':True, 'type':'open', 'details':'28c8', 'linenumber':360},
    {'keyword':'a', 'is_pair':True, 'type':'close', 'details':'28c8', 'linenumber':365},
    {'keyword':'a', 'is_pair':True, 'type':'open', 'details':'28c8', 'linenumber':370},
    {'keyword':'a', 'is_pair':True, 'type':'close', 'details':'28c8', 'linenumber':375},
    {'keyword':'a', 'is_pair':True, 'type':'open', 'details':'aowq', 'linenumber':400},
    {'keyword':'b', 'is_pair':True, 'type':'open', 'details':'pwiv', 'linenumber':520},
    {'keyword':'b', 'is_pair':True, 'type':'close', 'details':'pwiv', 'linenumber':528},
    {'keyword':'d', 'is_pair':False, 'details':'9393', 'linenumber':600},
    {'keyword':'b', 'is_pair':True, 'type':'open', 'details':'viao', 'linenumber':740},
    {'keyword':'b', 'is_pair':True, 'type':'close', 'details':'viao', 'linenumber':741},
    {'keyword':'b', 'is_pair':True, 'type':'open', 'details':'viao', 'linenumber':750},
    {'keyword':'b', 'is_pair':True, 'type':'close', 'details':'viao', 'linenumber':777},
    {'keyword':'a', 'is_pair':True, 'type':'close', 'details':'aowq', 'linenumber':822},
    {'keyword':'a', 'is_pair':True, 'type':'close', 'details':'83fi', 'linenumber':850},
    {'keyword':'a', 'is_pair':True, 'details':'iwiv', 'linenumber':990},
    {'keyword':'c', 'is_pair':False, 'details':'1212', 'linenumber':997},
]

我想做的是“配对”“最近的邻居”,其“关键字”匹配**,例如匹配括号嵌套,并将输出转储为某种标准化的制作- for-nesting-text 语法,如 XML 或 JSON。

我确实已经知道哪些关键字是“单独使用”与“应该匹配”,在我的输入中标记为“is_pair”。

我想为我组合的一对提供某种“行范围”......对于那些单行的,我不在乎它是否是“开始”和“结束”对其中有相同的数字,一个空的 End,一个完全不同的标签 (如我的例子),等等。

以下是输出可能的一些示例:

示例 1

<a start="5" end="990">
    iwiv
    <a start="200" end="850">
        83fi
        <a start="360" end="365">
            28c8
        </a>
        <a start="370" end="375">
            28c8
        </a>
        <a start="400" end="822">
            aowq
            <b start="520" end="528">
                pwiv
            </b>
            <d linenumber="600">
                9393
            </d>
            <b start="740" end="741">
                viao
            </b>
            <b start="750" end="777">
                viao
            </b>
        </a>
    </a>
</a>
<c linenumber="997">
    1212
</c>

示例 2

<a start="5" end="990" details="iwiv">
    <a start="200" end="850" details="83fi">
        <a start="360" end="365" details="28c8"/>
        <a start="370" end="375" details="28c8"/>
        <a start="400" end="822" details="aowq">
            <b start="520" end="528" details="pwiv"/>
            <d linenumber="600" details="9393"/>
            <b start="740" end="741" details="viao"/>
            <b start="750" end="777" details="viao"/>
        </a>
    </a>
</a>
<c linenumber="997" details="1212"/>

示例 3

[
    {
        'keyword':'a',
        'start':5,
        'end':990,
        'details':'iwiv', 
        'inner':[
            {
                'keyword':'a',
                'start':200,
                'end':850,
                'details':'83fi',
                'inner':[
                    {'keyword':'a', 'details':'28c8'},
                    {'keyword':'a', 'details':'28c8'},
                    {
                        'keyword':'a',
                        'start':400,
                        'end':822,
                        'details':'aowq',
                        'inner':[
                            {'keyword':'b', 'start':520, 'end':528, 'details':'pwiv'},
                            {'keyword':'d', 'linenumber':600, 'details':'9393'},
                            {'keyword':'b', 'start':740, 'end':741,  'details':'viao'},
                            {'keyword':'b', 'start':750, 'end':777,  'details':'viao'}
                        ]
                    }
                    ]
            }
        ]
    },
    {'keyword':'c', 'linenumber':997, 'details':'1212'}
]

我不一定需要有关编写 JSON 或 XML 文件的详细信息的帮助。

对于算法,尤其是 Python 而言,我不确定的是这项工作的“丛匹配”方面。

我如何将某些东西从“线性列表”转换为“嵌套”,其中每个具有 open 的元素都与同一关键字的下一个最近的 close 相匹配不是已经被更好的候选人“认领”了吗?

最佳答案

我建议用堆栈来解决这个问题。如果数据嵌套得当,就很容易解决。

但是,我会对嵌套不当的数据进行显式错误检查。因为如果您得到了错误的结束标记,那么难题就来了。

关于python - 将日志文件解析为其嵌套的开始和结束对的算法/Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55854046/

相关文章:

algorithm - 最近单词的最佳算法是什么

sql - 如何在SQL中选择相似的集合

android - 哪种算法适合情况分析?

c++ - 带有前缀和后缀查询的字符串集合的数据结构

python - Pandas:解析 Excel 中的合并标题列

python - 在 Python 中重现随机生成关卡的最简单方法是什么?

python - 定制融化/折叠 Pandas

python - 将否定文本转换为python中的文本

algorithm - 如何检查用户选择算法

java - 在 Tree 中插入一个节点,每个节点有 2 个以上的子节点