Replace the result found by re. findall () in python
The regular expression re module uses findall to find the ascii code. Therefore, when the comparison is replaced, the corresponding ascii code is also required to match successfully. The following programs are used to find files with male and female in the folder, replace male with 1, and replace female with 2.
# -*- coding: utf-8 -*- import fnmatchimport osimport codecsimport reimport sys def iterfindfiles(path, fnexp): for root, dirs, files in os.walk(path): for filename in fnmatch.filter(files, fnexp): yield os.path.join(root, filename) def fiterFiles(): path=raw_input("input dir:") filterfiletype=raw_input("input file filter type:") #quanjiao_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=re.sub('\((..?)\)\.dat','(2).dat',filename) print new_filename os.rename(filename,new_filename) def main(): fiterFiles() if __name__ == "__main__": main()