Python multithreading implementation of multi-line Cheng Wenben substitution function based on regular expression

Source: Internet
Author: User

#!/usr/bin/env pythonImportOSImportOs.pathImportSYSImportReImportShutilImportCSV fromMultiprocessingImportPool#Support muti thread map reduce functionSpecialchars= ['.','^','$','*','+','?','\\','[',']','|','(',')']g_max_thread_pool_size= 30#max thread pool sizedefdealspecialchars (str): s="'     forCinchStr:ifCinchspecialchars:s= S + ('['+ C +']')        Else: S= S+Creturnsdefreaddictsfromcsv (filePath):#Print FilePathDicts =dict () with open (FilePath,'RB') as Csvfile:dictsreader= csv. Dictreader (CSVFile, fieldnames=['SrcName','NewName'], Restkey=none, delimiter=',', quotechar='|')         forDinchDictsreader:#dicts[dealspecialchars (d[' srcname ')] = d[' newName ']dicts[d['SrcName']] = d['NewName']    returndictsbinaryextlist= ['. bmp','. avi','. Res','. xls','. doc','. DLL','. Lib','. BPL','. EXE','. CHM']replacedicts= {R"rnc820v400r008c00spc500": R" the" }  defapplyreplace (str, keys, replacedicts): Ret=Str forPatterninchKeys:Try: Ret=re.sub (pattern, Replacedicts[pattern], ret)except:            Print "Unexpected error applyreplace (str, keys, replacedicts):", str,finally:            PrintSTR, retreturnretdefneedreplace (str, keys): forPatterninchKeys:#print pattern, str        Try:            ifRe.search (Pattern, str):returnTrueexcept:            Print "Unexpected error needreplace (str, keys):"Str':'    returnFalse defaultextlist= ['. txt','. XML']  defFindFile (Srcdir, filter =None):if(Filter = =None): Filter=defaultextlist filelist= []     forNameinchOs.listdir (srcdir): FullPath= Srcpath +'\\'+nameifOs.path.isdir (FullPath): Filelist.append (FindFile (fullPath))Else:            ifOs.path.splitext (FullPath) [1].lower ()inchfilter:filelist.append (FullPath)returnfilelistdefReplaceallstrinfile (file, dicts, keys, filter =defaultextlist):Print 'replaceallstrinfile:file-', File,'begin!'        forKeyinchKeys:PrintKey, Dicts[key]#For TestFullPath =fileifOs.path.splitext (FullPath) [1].lower () not inchFilter:return    #print ' Convert file: ', FullPath    Try: Srcfile= Open (FullPath,'R') Tempfile= fullpath+'Temp'DestFile= Open (Tempfile,'W') Needrewrite=FalseTry:             forLineinchSrcfile:ifNeedreplace (line, keys): line=Applyreplace (line, keys, dicts) Destfile.write (line) srcfile.close () destfile.c Lose () Os.remove (FullPath) os.rename (Tempfile, FullPath)Print 'Convert file:', FullPath,'success!'        except:            Print 'Convert file:', FullPath,'failed!'srcfile.close () destfile.close () os.remove (tempfile)except:        Print 'Convert file:', FullPath,'failed!'        returnFalsereturnTruedefReplaceallstrinfilebyrows (Srcfile, csvfilepath, MaxRow = 10):    ifMaxRow < 1:        returnfalse Dicts=readdictsfromcsv (Csvfilepath) keys= (sorted (Dicts.keys (), key=LambdaKey:len (key), reverse=true))#reverse keys by elements ' lengthLength =len (keys) rows= Range (Length/maxrow + 1)     forIinchrows:replaceallstrinfile (Srcfile, dicts, keys[(i*maxrow):(i+1) *MaxRow]) Print ' '. Join (keys[(i*maxrow):(i+1) *MaxRow])deff (x):returnReplaceallstrinfilebyrows (X[0], x[1])if __name__=="__main__": Args=SYS.ARGVifLen (args) <> 3:        Print " "Usage:python testcsv.py D:\ss\temp.csv D:\ss\test" "Exit Csvfilepath= Args[1] Srcpath= Args[2] FileList=FindFile (Srcpath)#print filelist, Len (filelist)Dataitems= []     forFileinchfilelist:dataItems.append ([File, Csvfilepath])#replaceallstrinfilebyrows (dataitems[0][0], dataitems[0][1])Pool_size =g_max_thread_pool_sizeifLen (filelist) <g_max_thread_pool_size:pool_size=len (filelist) Pool= Pool (processes=pool_size)#Muti ThreadPool.map (F, Dataitems)#print Result.get (timeout=10)    #Pool.map  

Python multithreading implementation of multi-line Cheng Wenben substitution function based on regular expression

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.