Python - 正则表达式在方法中的返回方式不同于它自己

时间:2013-09-30 17:59:59

标签: python regex python-2.7 file-io directory

这需要一个目录(下面的文件的示例列表)并解析任何匹配正确格式的内容(下面),修复任何带有额外或缺少空格的内容(这里是问题),并附加任何带有区别符号的内容。问题是正则表达式本身就可以正常工作,但我的checkProper()方法可能存在一些问题,这对我来说并不是很明显。它可以分离已经采用正确格式的文件(在循环的第一部分),但在检查修复的任何内容时似乎不起作用。似乎问题是在某个地方循环而不是修复正则表达式(re.sub),因为我已经尝试了其他几种方法并得到了相同的结果(goodFix []为空)。这应该是修复这些文件,但事实并非如此。

正确的格式:

201308 - (82608) - MAC 2233-007-Methods of Calculus - Lastname, Firstname.pdf

目录中的文件样本列表:

201308 (82608)  MAC 2233-007-Methods of Calculus - Lname, Lee.txt
201308 - (12345) - ABC 2233L-007-course Name - last, first.txt
201308 - (12345) - XYZ 2233L-007-course Name 1 - last, first.txt
201308 - (82422) - MAA 5228-001- Introductory Analysis 1 - Zhang, Xiaodong.txt
201308 - (82429) - MAC 1105-004-College Algebra - Somelastname, Jesse.txt
201308 - (82490) - MAC 2311-003-Calculus and Analytic Geometry 1 - Lastname, Heinrich.txt
201308 - (82608) - MAC 2233-007-Methods of Calculus - Lname, Lee.txt
201308 - (82609) - MAC 2233L-007-Methods of Calculus 1 - Lname, Lee.txt
201308 - (96144) - STA 3173-001 - Introduction to Biostatistics - Qian, Lianfen.txt
201308 - (96381) - MAT 1033-023 -I ntemediate Algebra - Escuder, Ana.txt
201308 - (96444) - MAC 2313-009 - Calculus and Analytic Geometry 3 - Locke, Stephen.txt
@ 201308  -  @ '  ;  ,, @ 45 - 12 - xyz - mno  -  123.txt

上面注意到“Qian,Escuder,Locke”是文件的示例,应该更正为正确的格式而不是。

方法:

def readDir(path1):
    return [ f for f in os.listdir(path1) if os.path.isfile(os.path.join(path1,f)) ]

def checkProper(f,term):
    return re.match(term + '\s-\s\(\d{5}\)\s-\s\w{3}\s\d{4}\w?-\d{3}-[^\.]+\s-\s[^\.]+\.txt', f)

def regexSubFix(f,term):
    return re.sub(term + '\s*-\s*(\(\d{5}\))\s*-\s*(\w{3}\s\d{4}\w?-\d{3}-(?:[^.\s]|\b\s\b)+)\s*-\s*([^.]+\.txt)$', r' - \1 - \2 - \3', f)

def regexFindallFix(f):
    return ' - '.join(str(elem) for elem in re.findall(r'^(\d+)\s*-\s*(\(\d+\))\s*-\s*(.*?)\s*-\s*(\S+,.*)$', f)[0])

def properFiles(dir1,term,path1):
    goodMatch = []; stillWrong = []; goodFix = [] #; fixed = ""
    for f in dir1:
        result = checkProper(f,term)
        if result: goodMatch.append(result.group(0))
        else:
            #fixed = re.sub(r"(?<=[0-9]) *- *(?=[^0-9a-zA-Z])", " - ", re.sub(r"(?<=[^0-9]) *- *(?=[0-9a-zA-Z])", " - ", f))
            #fixed = parseFix(f)
            #fixed = regexFindallFix(f)
            fixed = regexSubFix(f,term)
            print "^^^^^^   ",fixed
            if checkProper(fixed,term):
                os.rename(path1+'\\'+f, path1+'\\'+fixed); goodFix.append(f)
            else: os.rename(path1+'\\'+f, path1+'\\'+'@ '+f); stillWrong.append(f)
        #print "f ---- ",f
    goodToGo = len(goodMatch)+len(goodFix); total = len(dir1)
    print "%d total files. %d files in proper format. %f%% success rate."%(total,goodToGo,(goodToGo/(float(total)))*100.0)
    print "All files not in proper format are appended with @ to be clearly marked for the user."
    return goodMatch, goodFix, stillWrong

在main中调用时,readDir()提供要传递到properFiles()的目录中的文件列表。 regexSubFix()是现在使用的方法,但是使用regexFindallFix()和另一种方法,结果是相同的(并且没有附加到goodFix[])。 checkProper()似乎在其他所有内容中都正常工作,但最后一条if语句似乎没有捕获fixed个文件。

我正在学习Python,所以我很确定一个初级程序员可以很快发现这个问题,但是如果在这个问题上有任何专业人士,我确信这对他们来说很有用。

编辑: 这些文件有不正确的空格,但由于某种原因没有修复:

201308 - (82442) - MAC 1105 - 012 - College Algebra - Harmon, Drake.txt

2 个答案:

答案 0 :(得分:1)

好的,我想我发现了问题:

要修复可修复的问题,请将其设为regexSubFid def:

def regexSubFix(f,term):
    return re.sub(term + r'\s*-\s*(\(\d{5}\))\s*-\s*(\w{3}\s\d{4}\w?\s*-\s*\d{3}\s*-\s*(?:[^.\s]|\b\s\b)+)\s*-\s*([^.]+\.txt)$',
          lambda match: term+' - {0} - {1} - {2}'.format(match.group(1),
          re.sub(r'\s*-\s*', '-', match.group(2)),
          match.group(3)) ,
          f)

接下来,将if部分更改为:

if checkProper(fixed,term):
    goodFix.append(fixed)
else: stillWrong.append(fixed)

唯一剩下的就是你有这个文件成了这个:

201308 - (96381) - MAT 1033-023-I ntemediate Algebra - Escuder, Ana.txt

答案 1 :(得分:0)

看起来这是一个简单的逻辑反转:

if checkProper(fixed,term):
    os.rename(path1+'\\'+f, path1+'\\'+fixed); goodFix.append(f)

如果checkProper不匹配,这将用正确的版本替换名称 - 所以基本上,这段代码表示如果名称有效,请替换它,但如果它无效,请不管它。只需交换if:

的意义
if not checkProper(fixed,term):
    os.rename(path1+'\\'+f, path1+'\\'+fixed); goodFix.append(f)

你应该拥有它。