欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用python简单筛选数据

程序员文章站 2024-03-21 11:39:58
...

使用python简单筛选数据

使用前需要自行安装python环境,将pyw文件和待筛选文件放在同一个文件夹目录下。

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import time
import re

inFileName = "inFile.log";
outFileName = "outFile.txt"
outFileName0 = "outFile0.txt"
outFileName1 = "outFile1.txt"
outFileName2 = "outFile2.txt"
outFileName3 = "outFile3.txt"

def main():
    print 'analysis start......';
    print 'inFileName is : %s' %(inFileName)
    print 'outFileName is: %s' %(outFileName)

    document = open(outFileName,"w+");
    document0 = open(outFileName0,"w+");
    document1 = open(outFileName1,"w+");
    document2 = open(outFileName2,"w+");
    document3 = open(outFileName3,"w+");
    
    with open(inFileName) as log:
        line = log.readline();
        while line:
            matchObj = re.match(r'.*cnt:.*', line);
            if  matchObj:
                document.write(line);
                
            matchObj = re.match(r'.****:  11 11 11 11.*', line);
            if matchObj:
                document0.write(line);

            matchObj = re.match(r'.*换算后: 2..*', line);
            if matchObj:
                document1.write(line);

            matchObj = re.match(r'.*22222222.*', line);
            if matchObj:
                document2.write(line);

            matchObj = re.match(r'.*33333333.*', line);
            if matchObj:
                document3.write(line);
            line = log.readline();
            
    document.close();
    document0.close();
    
    print "analysis end......"

if __name__ == '__main__':
    main();


使用python简单筛选数据
F5运行后,可以将具有特别关键字的数据行分别记录在各个指定文件下
使用python简单筛选数据