Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def writeToFile(isOpen, fileName, output):
- if not isOpen:
- file = open(fileName + ".txt","a", buffering=MEG)
- isOpen = True
- file.write(output)
- def lineSearch_multi(id,inputFile, keyWord1,keyWord2,keyWord3, raw_str*):
- startTime = datetime.now() #time it
- global file1, file2, file3, file4, file5, file6, save_path #used to only open file when I have a match
- raw_str=list(raw_str) #convert tuple to list
- raw_str= ["^"+x for x in raw_str] #add regex ^ newline operator
- search_rgx=[re.sub(r'\*+',r'[\\s\\S]*?',raw_str) for raw_str in raw_str]
- search_rgx='|'.join(search_rgx) #convert to string and add | or operator
- if search_rgx: #makesure the regex input is not blank else will search everything
- with open(inputFile, buffering=MEG, encoding="utf-8") as in_f:
- line=in_f.readline().lower() #read the first line
- while(line !=""): #while line is not the end
- line=line.lower() #covert to lowercase always
- out = re.match(search_rgx,line, re.IGNORECASE)
- if out: #if the line matches the initial regex
- if keyWord2: #if keyword2 is actually available
- if (keyWord2 in line): #if keyword2 is in the current line
- if ((keyWord1 in line and keyWord2 in line):
- #found a result, now see if the file is open then write
- writeToFile(file1,os.path.join(save_path, id+"_"+ keyWord2+"_and_"+keyWord1+"_result"), line)
- elif (keyWord1 in line or keyWord2 in line):
- #found a result, now see if the file is open
- writeToFile(file6,os.path.join(save_path, id+"_"+ keyWord2+"_or_"+keyWord1+"_result"), line)
- elif keyWord3:
- if (keyWord3 in line and keyWord2 in line):
- #found a result, now see if the file is open
- writeToFile(file2,os.path.join(save_path,id+"_"+ keyWord2+keyWord3+"_both_result"), line)
- else:
- writeToFile(file3,os.path.join(save_path,id+"_"+ keyWord2+"_only_result"), line)
- if keyWord3: #if keyword3 is actually available
- if (keyWord3 in line):
- if (keyWord1 in line and keyWord3 in line):
- #found a result, now see if the file is open
- writeToFile(file4,os.path.join(save_path,id+"_"+ keyWord3+keyWord1+"_result"), line)
- else:
- writeToFile(file5,os.path.join(save_path,id+"_"+ keyWord3+"_only_result"), line)
- line=in_f.readline() #go to nextline
- file1= file2= file3= file4= file5= False #set all files back to false
Add Comment
Please, Sign In to add comment