Replace the result found in Python re.findall ()

Source: Internet
Author: User

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

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.