pyparsing:定制自己的解析器

Posted 李雷雷

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyparsing:定制自己的解析器相关的知识,希望对你有一定的参考价值。

在工作中,经常需要解析不同类型的文件,常用的可能就是正则表达式了,简单点的,可能会使用awk。这里要推荐一种比较小众的方式,使用pyparsing来解析文件。

pyparsing可以做些什么呢?主要可以相当方便地定制自己的tokenizer,因此可以很容易拓展,实现自己的parser

下面看一个traceview的解析例子

16803 AsyncTask #3
16804 pool-2-thread-5
16806 pool-3-thread-1
16807 uil-pool-2-thread-1
16808 uil-pool-2-thread-2
16809 uil-pool-2-thread-3
16810 uil-pool-2-thread-4
Trace (threadID action usecs class.method signature):
16736 xit         0 ..dalvik.system.VMDebug.startMethodTracingFilename (Ljava/lang/String;IIZI)V	VMDebug.java
16804 xit         0 ..com.android.org.conscrypt.NativeCrypto.EVP_DigestUpdate (Lcom/android/org/conscrypt/OpenSSLDigestContext;[BII)V	NativeCrypto.java
16736 xit       218 .dalvik.system.VMDebug.startMethodTracing (Ljava/lang/String;IIZI)V	VMDebug.java
16736 xit       225 android.os.Debug.startMethodTracing (Ljava/lang/String;II)V	Debug.java
16736 xit       230-android.os.Debug.startMethodTracing (Ljava/lang/String;I)V	Debug.java
16736 xit       266-java.lang.reflect.Method.invoke (Ljava/lang/Object;[Ljava/lang/Object;Z)Ljava/lang/Object;	Method.java
16804 ent       528 ..java.lang.ClassLoader.loadClass (Ljava/lang/String;)Ljava/lang/Class;	ClassLoader.java
16804 ent       543 ...java.lang.ClassLoader.loadClass (Ljava/lang/String;Z)Ljava/lang/Class;	ClassLoader.java
16804 ent       548 ....java.lang.ClassLoader.findLoadedClass (Ljava/lang/String;)Ljava/lang/Class;	ClassLoader.java
16804 ent       567 .....java.lang.BootClassLoader.getInstance ()Ljava/lang/BootClassLoader;	ClassLoader.java
16804 xit       576 .....java.lang.BootClassLoader.getInstance ()Ljava/lang/BootClassLoader;	ClassLoader.java
16804 xit       681 ....java.lang.ClassLoader.findLoadedClass (Ljava/lang/String;)Ljava/lang/Class;	ClassLoader.java
16804 ent       689 ....com.uc.base.aerie.hack.ClassLoaderSupport$a.loadClass (Ljava/lang/String;Z)Ljava/lang/Class;	ProGuard
16804 ent       704 .....java.lang.ClassLoader.getParent ()Ljava/lang/ClassLoader;	ClassLoader.java
8
16804 ent       726 ......java.lang.BootClassLoader.loadClass (Ljava/lang/String;Z)Ljava/lang/Class;	ClassLoader.java
16804 ent       730 .......java.lang.ClassLoader.findLoadedClass (Ljava/lang/String;)Ljava/lang/Class;	ClassLoader.java
16804 ent       734 ........java.lang.BootClassLoader.getInstance ()Ljava/lang/BootClassLoader;	ClassLoader.java
16804 xit       740 ........java.lang.BootClassLoader.getInstance ()Ljava/lang/BootClassLoader;	ClassLoader.java
16804 xit       754 .......java.lang.ClassLoader.findLoadedClass (Ljava/lang/String;)Ljava/lang/Class;	ClassLoader.java
16804 xit       759 ......java.lang.BootClassLoader.loadClass (Ljava/lang/String;Z)Ljava/lang/Class;	ClassLoader.java
16804 xit       763 .....java.lang.ClassLoader.loadClass (Ljava/lang/String;)Ljava/lang/Class;	ClassLoader.java

这是一部分转换后的原始日志,格式比较标准,因此可以这么定制

import os

from pyparsing import Word, nums, Combine, alphas, Literal, ZeroOrMore, Group,     Suppress


semiFlag = Literal(";")
dotFlag = Suppress(Literal("."))
multiDot = ZeroOrMore(dotFlag)

threadID =Word(nums, max=5)
actionField = Word(alphas)
usecsField = Word(nums, max=8)

clsField = Word(alphas+".")
methodField = Combine("(" + ZeroOrMore(Word(alphas + ";/")) + ")" + Word(alphas + "/") + semiFlag)

regex = threadID + actionField + usecsField + multiDot + Group(clsField + methodField) + clsField


with open(os.path.join(os.getcwd(), "StepBeforeFirstDraw_o.txt"), "rb") as f:
    lineno = 0
    flag = 0
    while 1:
        line = f.readline()
        lineno += 1
        if "threadID action usecs" in line:
            flag = lineno
            continue
        if flag > 0:
            try:
                regex.parseString(line).toXML("")
            except Exception as e:
                pass

解析结果为:

/usr/bin/python2.7 /home/alex/workspace/virtual_space/project/calclex.py
[‘16804‘, ‘ent‘, ‘528‘, [‘java.lang.ClassLoader.loadClass‘, ‘(Ljava/lang/String;)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘543‘, [‘java.lang.ClassLoader.loadClass‘, ‘(Ljava/lang/String;Z)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘548‘, [‘java.lang.ClassLoader.findLoadedClass‘, ‘(Ljava/lang/String;)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘567‘, [‘java.lang.BootClassLoader.getInstance‘, ‘()Ljava/lang/BootClassLoader;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘xit‘, ‘576‘, [‘java.lang.BootClassLoader.getInstance‘, ‘()Ljava/lang/BootClassLoader;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘xit‘, ‘681‘, [‘java.lang.ClassLoader.findLoadedClass‘, ‘(Ljava/lang/String;)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘704‘, [‘java.lang.ClassLoader.getParent‘, ‘()Ljava/lang/ClassLoader;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘726‘, [‘java.lang.BootClassLoader.loadClass‘, ‘(Ljava/lang/String;Z)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘730‘, [‘java.lang.ClassLoader.findLoadedClass‘, ‘(Ljava/lang/String;)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘734‘, [‘java.lang.BootClassLoader.getInstance‘, ‘()Ljava/lang/BootClassLoader;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘xit‘, ‘740‘, [‘java.lang.BootClassLoader.getInstance‘, ‘()Ljava/lang/BootClassLoader;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘xit‘, ‘754‘, [‘java.lang.ClassLoader.findLoadedClass‘, ‘(Ljava/lang/String;)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘xit‘, ‘759‘, [‘java.lang.BootClassLoader.loadClass‘, ‘(Ljava/lang/String;Z)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘xit‘, ‘763‘, [‘java.lang.ClassLoader.loadClass‘, ‘(Ljava/lang/String;)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘xit‘, ‘771‘, [‘java.lang.ClassLoader.loadClass‘, ‘(Ljava/lang/String;Z)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘xit‘, ‘774‘, [‘java.lang.ClassLoader.loadClass‘, ‘(Ljava/lang/String;)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘809‘, [‘java.lang.ClassLoader.loadClass‘, ‘(Ljava/lang/String;)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘814‘, [‘java.lang.ClassLoader.loadClass‘, ‘(Ljava/lang/String;Z)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘818‘, [‘java.lang.ClassLoader.findLoadedClass‘, ‘(Ljava/lang/String;)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘822‘, [‘java.lang.BootClassLoader.getInstance‘, ‘()Ljava/lang/BootClassLoader;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘xit‘, ‘827‘, [‘java.lang.BootClassLoader.getInstance‘, ‘()Ljava/lang/BootClassLoader;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘xit‘, ‘842‘, [‘java.lang.ClassLoader.findLoadedClass‘, ‘(Ljava/lang/String;)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘853‘, [‘java.lang.ClassLoader.getParent‘, ‘()Ljava/lang/ClassLoader;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘xit‘, ‘857‘, [‘java.lang.ClassLoader.getParent‘, ‘()Ljava/lang/ClassLoader;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘861‘, [‘java.lang.ClassLoader.loadClass‘, ‘(Ljava/lang/String;)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘865‘, [‘java.lang.BootClassLoader.loadClass‘, ‘(Ljava/lang/String;Z)Ljava/lang/Class;‘], ‘ClassLoader.java‘]
[‘16804‘, ‘ent‘, ‘869‘, [‘java.lang.ClassLoader.findLoadedClass‘, ‘(Ljava/lang/String;)Ljava/lang/Class;‘], ‘ClassLoader.java‘]

这样已经很方便去做二次处理了,而且解析规则的可读性也会比正则的强。

以上是关于pyparsing:定制自己的解析器的主要内容,如果未能解决你的问题,请参考以下文章

PyParsing 使用大括号和特定标头解析嵌套循环

pyparsing用分号而不是逗号解析csv文件

Android 逆向使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 创建反汇编解析器实例对象 | 设置汇编解析器显示细节 )(代码片段

Gradle 引入本地定制 jar 包

NodeJs GraphQL 片段解析器

无法让 pyparsing Dict() 返回嵌套字典