디렉토리의 파일 묶음에서 정규 표현식을 사용하여 문자열을 처리하고 있습니다. 파일의 각 줄마다 일련의 try-statements를 적용하여 패턴과 일치 시키면 입력을 변환합니다. 각 줄을 분석 한 후 새 파일에 기록합니다. 나는 이 try-else 다음에 if-statements (나는 그림으로 여기에 두 개만 포함했다)를 사용했다. 내 문제는 여기에 몇 가지 파일을 처리 한 후 스크립트가 너무 느려서 프로세스가 거의 완전히 정지된다는 것입니다. 내 코드에서 느려지는 원인을 알 수는 없지만 try-else + if-statement의 조합이라고 느낍니다. 데이터가 합리적인 속도로 처리되도록 변환을 어떻게 능률화 할 수 있습니까?파이썬에서 더 빠른 처리를 위해 일련의 try-except + if-statements를 간소화합니다.
아니면 같은 정도로 메모리에 세금을 부과하지 않는 효율적인 반복기가 필요합니까?
모든 의견을 보내 주시면 감사하겠습니다.
import re
import glob
fileCounter = 0
for infile in glob.iglob(r'\input-files\*.txt'):
fileCounter += 1
outfile = r'\output-files\output_%s.txt' % fileCounter
with open(infile, "rb") as inList, open(outfile, "wb") as outlist:
for inline in inlist:
inword = inline.strip('\r\n')
#apply some text transformations
#Transformation #1
try: result = re.match('^[AEIOUYaeiouy]([bcćdfghjklłmnńprsśtwzżź]|rz|sz|cz|dz|dż|dź|ch)[aąeęioóuy](.*\[=\].*)*', inword).group()
except: result = None
if result == inword:
inword = re.sub('(?<=^[AEIOUYaeiouy])(?=([bcćdfghjklłmnńprsśtwzżź]|rz|sz|cz|dz|dż|dź|ch)[aąeęioóuy])', '[=]', wbWord)
#Transformation #2 etc.
try: result = re.match('(.*\[=\].*)*(\w?\w?)[AEIOUYaąeęioóuy]\[=\][ćsśz][ptkbdg][aąeęioóuyrfw](.*\[=\].*)*', inword).group()
except: result = None
if result == inword:
inword = re.sub('(?<=[AEIOUYaąeęioóuy])\[=\](?=[ćsśz][ptkbdg][aąeęioóuyrfw])', '', inword)
inword = re.sub('(?<=[AEIOUYaąeęioóuy][ćsśz])(?=[ptkbdg][aąeęioóuyrfw])', '[=]', inword)
outline = inword + "\n"
outlist.write(outline)
print "Processed file number %s" % fileCounter
print "*** Processing completed ***"
https://codereview.stackexchange.com/ –