zoukankan      html  css  js  c++  java
  • Python小工具-查找关键字

    跑Monkey时,可能会产生大量日志,如果一个一个找的话又太浪费时间,所以通过Python编写了一段代码来查找需要的关键字,如果没有我们想要的关键字,就完全没有必要再去将日志翻一遍了

    exc = ['Touch','CRASH','ANR','died']  # 要查找的关键字
    
    with open('C:\Users\monkey_01.txt', encoding='utf-8') as f:    
        if os.path.exists('copy.txt'):                   
            with open('copy.txt','r+') as f2:    # 清空文件
                res = f2.readlines()
                print(res)
                f2.seek(0)
                f2.truncate()
        with open('copy.txt', 'at') as f1:       
            f1.write('崩溃日志:
    ')
        dic_key = {}
        for key in exc:
            dic_key[key] = 0
        for line in f:
            for key in exc:
                if key in line:
                    dic_key[key] =dic_key[key] + line.count(key)
                    # print(line)
                    with open('copy.txt','at') as f:
                        f.write(line)
        # print(dic_key)
        with open('copy.txt','at') as f:                  # 写入文件
            f.write("分析结果:
    ")
            f.write("------------------------------
    ")
            for key, value in dic_key.items() :
                f.write("关键字{}出现的次数:{}
    ".format(key, value))
  • 相关阅读:
    Linux:mv命令
    Linux:cp -rp
    Linux:sed命令
    Linux:cut命令...未完待续
    Linux:xargs命令
    python动态获取对象的属性和方法 (转载)
    python 继承中的super
    HTTP认证机制(翻译)
    技术名词解释
    设计模式之代理模式
  • 原文地址:https://www.cnblogs.com/jiablogs/p/11134438.html
Copyright © 2011-2022 走看看