0

我正在预处理一组文件,其中一个文件包含在另一个文件中,其中包含标签,如下所示:

档案A

include file B
include file C
 contents of file A
include file D
 contents of file A

在这里,我需要用每个相应文件的内容替换包含标签。就像编译器所做的一样。我有两个功能

    def parseContent(self, rdbFilePath, content):
        finalContent = self.removeComments(content)
        includeFileSearch = re.compile(r'(?P<tag>(\s)*include+(\s*)\"(\s*)(?P<file>[a-zA-Z0-9\.\_/]*)\")')
        for includes in includeFileSearch.finditer(finalContent):
            finalContent = re.sub(includes.group('tag'),self.parseIncludes(rdbFilePath, includes.group('file')), finalContent)
        return finalContent

    def parseIncludes(self, rdbFilePath, file):
        path = rdbFilePath + "/" + file
        f = open(path)
        pathDir = os.path.dirname(path)
        includedFileContent = self.parseContent(pathDir, f.read())
        return includedFileContent

如您所见,函数parseContentparseIncludes相互递归调用以替换每个文件中的所有包含标记。逻辑工作正常。但是执行起来需要一点时间。有没有更好的方法可以用更少的执行时间来做同样的事情?

4

0 回答 0