zoukankan      html  css  js  c++  java
  • python自定义方法处理日志文件

    从命令行界面拷贝的内容包含过个">>>",函数的作用是用正则把每两个">>>"之间的字符取出来,然后把包含“Traceback...”的字符的内容去掉,再写到另一个文件中

    代码:

    #coding=utf-8
    import re
    import os
    def clearContent(fileName):
        result=[]
        with open(fileName) as fp:
            content=fp.read()
        L=re.findall( r'(?<=>>>).+?(?=>>>)' , content,re.M|re.DOTALL)
        print "len(L):",len(L)
        for i in L:
            if "Traceback" not in i:
                result.append(i)
        print "len(result):",len(result)
        with open("%s_new1.txt"%os.path.splitext(fileName)[0],"w") as fp1:
            for i in result:
                fp1.write(i)
        print "Done! please find the new file: %s_new1.txt"%os.path.splitext(fileName)[0]
        return ""

    clearContent("d:\re.txt")


    结果:

  • 相关阅读:
    hgoi#20191101
    hgoi#20191031
    hgoi#20191030
    hgoi#20191029-2
    RMQ (Range Minimum/Maximum Query)
    数学浅谈-组合数与数学期望
    重庆NK十日行-知识点汇总
    分块
    STL—algorithm与Map容器
    搜索—迭代加深
  • 原文地址:https://www.cnblogs.com/xiaxiaoxu/p/9750719.html
Copyright © 2011-2022 走看看