tetester

Untitled

May 24th, 2020
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. def writeToFile(isOpen, fileName, output):
  2.     if not isOpen:
  3.         file = open(fileName + ".txt","a", buffering=MEG)
  4.         isOpen = True
  5.     file.write(output)
  6.  
  7. def lineSearch_multi(id,inputFile, keyWord1,keyWord2,keyWord3, raw_str*):
  8.     startTime = datetime.now() #time it
  9.     global file1, file2, file3, file4, file5, file6, save_path #used to only open file when I have a match
  10.  
  11.     raw_str=list(raw_str) #convert tuple to list
  12.     raw_str= ["^"+x for x in raw_str] #add regex ^ newline operator
  13.     search_rgx=[re.sub(r'\*+',r'[\\s\\S]*?',raw_str) for raw_str in raw_str]
  14.     search_rgx='|'.join(search_rgx) #convert to string and add | or operator
  15.  
  16.     if search_rgx: #makesure the regex input is not blank else will search everything
  17.  
  18.         with open(inputFile, buffering=MEG, encoding="utf-8") as in_f:
  19.             line=in_f.readline().lower() #read the first line
  20.            
  21.             while(line !=""): #while line is not the end
  22.                     line=line.lower() #covert to lowercase always
  23.                    
  24.                     out = re.match(search_rgx,line, re.IGNORECASE)
  25.                     if out: #if the line matches the initial regex
  26.  
  27.                         if keyWord2: #if keyword2 is actually available
  28.  
  29.                             if (keyWord2 in line): #if keyword2 is in the current line
  30.                                 if ((keyWord1 in line and keyWord2 in line):
  31.                                     #found a result, now see if the file is open then write
  32.                                     writeToFile(file1,os.path.join(save_path, id+"_"+  keyWord2+"_and_"+keyWord1+"_result"), line)
  33.                                    
  34.                                 elif (keyWord1 in line or keyWord2 in line):
  35.                                     #found a result, now see if the file is open
  36.                                     writeToFile(file6,os.path.join(save_path, id+"_"+  keyWord2+"_or_"+keyWord1+"_result"), line)
  37.  
  38.                                 elif keyWord3:
  39.                                     if (keyWord3 in line and keyWord2 in line):
  40.                                         #found a result, now see if the file is open
  41.                                         writeToFile(file2,os.path.join(save_path,id+"_"+  keyWord2+keyWord3+"_both_result"), line)
  42.  
  43.                                 else:
  44.                                     writeToFile(file3,os.path.join(save_path,id+"_"+  keyWord2+"_only_result"), line)
  45.  
  46.                         if keyWord3: #if keyword3 is actually available
  47.                             if (keyWord3 in line):
  48.                                 if (keyWord1 in line and keyWord3 in line):
  49.                                     #found a result, now see if the file is open
  50.                                     writeToFile(file4,os.path.join(save_path,id+"_"+  keyWord3+keyWord1+"_result"), line)
  51.  
  52.                                 else:
  53.                                     writeToFile(file5,os.path.join(save_path,id+"_"+  keyWord3+"_only_result"), line)
  54.  
  55.                     line=in_f.readline() #go to nextline
  56.     file1= file2= file3= file4= file5= False #set all files back to false
Add Comment
Please, Sign In to add comment