Replace the result found by re. findall () in python

Source: Internet
Author: User

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()


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.