python过滤log数据并输出到txt文件
import re import sys import csv #好像没用,忘了 import os fileDir = "C:/Users/Administrator/Desktop/过滤数据/log_orig/" fileDirN = "C:/Users/Administrator/Desktop/过滤数据/cpu/" output=sys.stdout for file in os.listdir(fileDir): with open(fileDir+file,'r',encoding='UTF-8',errors='ignore') as f: fileName = re.split('\.',file) outputfile=open(fileDirN+fileName[0]+'.txt','w') sys.stdout=outputfile pattern = re.compile(r'(.*)(cpu used \d*)')#正则表达式 print (' ',end="")#多输入一个空格,每行数据保持一致 for line in f: m=pattern.match(line) if(m is not None): print (line,end=' ') outputfile.close() sys.stdout=output