另一个Python范围问题 - 丢失信息进入if语句

时间:2010-06-29 14:17:59

标签: python scope

不确定我是否遗漏了一些明显的东西,但这里发生了什么: 我有一个包含几个RegEx对象的python 2.4.3脚本。其中一个正则表达式对象正在搜索字符串中的所有匹配项(tMatchList)。即使tMatchList不为null,它也会在'if p:'步骤后打印一个空集。即使在'if p:'步骤之前正确打印,也会出现此问题。我认为这可能是一个范围问题,但所有事情都被宣布为&包含在一个函数中。我不太清楚'if p:'步骤是如何看不到tMatchList的。我也可以在if语句之后打印tMatchList。

tMatchList = []
for lines in r:
    linecount += 1

    tMatchList = self._testReplacePDFTag.findall(lines)

    p = self._pdfPathRegex.search(lines)
    print tMatchList   #tMatchList is printing just fine here if it has any elements
    if p:
        print tMatchList #now it's empty, 
                         #even if it printed elements in prior statement
        lines = .....
    else:
        <something else gets done>
    print tMatchList #now it prints again

为想要看到它的人包括整个功能定义....

def FindFilesAndModifyPDFTag(self, inRootDirArg, inRollBackBool):
    for root, dirs, files in os.walk(inRootDirArg):
        for d in dirs: 
            if d.startswith('.'):#excludes directories that start with '.'
                continue
        for file in files:
            if os.path.splitext(file)[1] == self._fileExt:
                #Backup original. just do it
                shutil.copy2(os.path.join(root, file), os.path.join(root, file)+"~") 
                r = open(os.path.join(root, file)+"~", "r")
                f = open(os.path.join(root, file), "w")

                linecount = 0
                tMatchList = []
                for lines in r:
                    linecount += 1

                    tMatchList = self._testReplacePDFTag.findall(lines)
                    t = self._testReplacePDFTag.search(lines)

                    #find pdf path(s) in line                    
                    pMatchList = self._pdfPathRegex.findall(lines)
                    p = self._pdfPathRegex.search(lines)
                    #fix the pdf tracking code 
                    print id(tMatchList), "BEFORE"
                    if p:   
                        print id(tMatchList), "INSIDE"
                        lines = self.processPDFTagLine(pMatchList, lines, linecount, file, tMatchList)
                    else:
                        lines = self.processCheckMetaTag(lines, linecount, file)
                        #print id(tMatchList), "INSIDE ELSE"

                         print id(tMatchList), "AFTER"
                         f.writelines(lines)

                f.close()
                r.close()
                os.remove(os.path.join(root, file)+"~")

enter code here

2 个答案:

答案 0 :(得分:0)

findall可能无法创建列表对象。如果它是某种生成器函数,那么它有一个通过遍历结果“消耗”的值。

在使用此函数产生的结果后,没有更多结果。

tMatchList = self._testReplacePDFTag.findall(lines)

p = self._pdfPathRegex.search(lines)
print tMatchList   #tMatchList is printing just fine here if it has any elements
if p:
    print tMatchList #now it's empty, 

试试这个。

tMatchList = list( self._testReplacePDFTag.findall(lines) )

答案 1 :(得分:0)

所以这就是最终'解决'这个问题: 我移动了tMatchList findall()行来跟随搜索。然后我添加了一个'if t:'语句。现在,当我打印出来时,我在'if p:'语句中看到了tMatchList的内容。所以问题现在已经解决了,但我认为有一些关于re模块(?)或评估空列表对象的行为问题我不知道。

#original code ....
linecount += 1

#this is modified section
t = self._testReplacePDFTag.search(lines)
if t:
    tMatchList = self._testReplacePDFTag.findall(lines)

#end modified section

pMatchList = self._pdfPathRegex.findall(lines)
相关问题