Python 过滤文件

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

import requests
import re
import os
import datetime
import time
from builtins import str
from fileinput import filename

def filterStr(str):
    #return str.replace('setErrorDetail(', '').replace('"', '')\
     #   .replace('$t', '').replace('(', '').replace(')', '').replace('\'', '').replace('setErrorSolution','')
     return str


def getFiles(rootPath, fileList):
    dirOrFiles = os.listdir(rootPath)
    for root, dirs, files in os.walk(rootPath):
        for file in files:
            fileList.append(os.path.join(root, file))
    # print(dirOrFiles.__len__())
    # for dirFile in dirOrFiles:
    #     dirFilePath = os.path.join(rootPath, dirFile)
    #     if os.path.isfile(dirFilePath):
    #         if os.path.splitext(dirFilePath)[1] == '.java':
    #             fileList.append(dirFilePath)
    #             #print(dirFilePath)

def getResStrings(fileName, regexStr, resLists):
    f = open(fileName,'r',encoding='UTF-8')
    content = f.read()
    pattern = re.compile(regexStr)
    result = pattern.findall(content)
    for str in result:
        resLists.append(filterStr(str))

fileList = []
resList = []
headList = []
endList = []
lists = []

getFiles(r'D:\estsh\doc\1', fileList)

# 获取枚举头
for fileName in fileList:
    regexStr = r"enum(?:.|\r|\n)*?{"
    getResStrings(fileName, regexStr, resList)
    
# 获取枚举第二级
for fileName in fileList:
    regexStr = r"{(?:.|\r|\n)*?;"
    getResStrings(fileName, regexStr, endList)

for resStr in resList:
    headList.append(resStr.replace("enum ", "").replace(" {", ":"))

endResult = []
for resStr in endList:
    endResult.append(resStr.replace("{\n", ""))
    
results = []
strList = []
for resStr in endResult:
    str = resStr.replace('\n', '').split('),')
    results.append(str)

secondList = []
for i in range(len(results)):
    strs = []
    for str in results[i]:
        pattern = re.compile("(\S+?)\(")
        result = pattern.findall(str)
        for st in result:
            strs.append(st)
    print(strs)
    secondList.append(strs)

for i in range(len(headList)):
    for str in secondList[i]:
        print(headList[i] + str)      



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值