In the regular expression re module, the ASCII code is found in the FindAll, so the corresponding ASCII code is needed to match the replacement. The following program is to find the folder under the file name contains the male and female files, and replace the male to 1, the woman replaced by 2 of the program
#-*-Coding:utf-8-*-import fnmatchimport osimport codecsimport reimport sysdef iterfindfiles (Path, fnexp): for Root, dirs, files in Os.walk (path): for filename in fnmatch.filter (Files, fnexp): Yield os.path.join (root, fi Lename) def fiterfiles (): Path=raw_input ("Input dir:") filterfiletype=raw_input ("Input file filter Type:") #qu Anjiao_2_banjiao (path) for filename in Iterfindfiles (path,filterfiletype): Singerlist=re.findall (' \ (...) \) \.dat ', filename) for singername in Singerlist:if (singername== ' \xc4\xd0 '): Singername= ' 1 ' new_filename=re.sub (' \ (..?) \) \.dat ', ' (1). dat ', filename) print new_filename os.rename (filename,new_filename) #print singername elif (singername== ' \xc5\xae '): Singername= ' 2 ' new_filename=r E.sub (' \ (..?) \) \.dat ', ' (2). dat ', filename) print new_filename os.rename (filenAme,new_filename) def main (): Fiterfiles () if __name__ = = "__main__": Main ()
Replace the result found in Python re.findall ()