How to ignore case of a word while searching for it in a text file and copying into another

StackOverflow https://stackoverflow.com/questions/21312602

  •  01-10-2022
  •  | 
  •  

Question

I am trying to write a program in python which searches for user specified words in a txt file and copies the selected lines containing that word into another file.

Also the user will have an option to exclude any word.

(e.g Suppose the user searches for the word "exception" and want to exclude the word "abc", then the code will only copy the lines which has "exception" in it but not "abc").

Now all the work will be done from the command prompt.

The input would be:

file.py test.txt(input file) test_mod.txt(output file) -e abc(exclude word denoted by -e)-s exception(search word denoted by -s) Now the user will have an option to enter multiple exclude words and multiple search words.

I have done the program using the argparse module and it runs. My problem is it only takes lower case words as search or exclude words. That is if I type "exception" as search word, it does not find "Exception" or "EXCEPTION". How do I solve this prob? I want to ignore case on both search and exclude words. Here's my code as of now:

import sys
import os
import argparse
import tempfile
import re

def main(): #main method

 try:

  parser = argparse.ArgumentParser(description='Copies selected lines from files') #Defining the parser
  parser.add_argument('input_file')  #Adds the command line arguments to be given 
  parser.add_argument('output_file')
  parser.add_argument('-e',action="append")
  parser.add_argument('-s',action="append")
  args = parser.parse_args() #Parses the Arguments
  user_input1 = (args.e)    #takes the word which is to be excluded.
  user_input2 = (args.s)    #takes the word which is to be included.

  def include_exclude(input_file, output_file, exclusion_list=[], inclusion_list=[]):  #Function which actually does the file writing and also handles exceptions
      if input_file == output_file: 
          sys.exit("ERROR! Two file names cannot be the same.")
      else:
          try: 
              found_s = False  #These 3 boolean variables will be used later to handle different exceptions.
              found_e = False
              found_e1 = True
              with open(output_file, 'w') as fo:  #opens the output file
                  with open(input_file, 'r') as fi: #opens the input file
                       for line in fi:     #reads all the line in the input file
                           if user_input2 != None:


                               inclusion_words_in_line = map(lambda x: x in line, inclusion_list)#Mapping the inclusion and the exclusion list in a new list in the namespace  
                               if user_input1 != None and user_input2 != None:                   #This list is defined as a single variable as condition operators cannot be applied to lists
                                  exclusion_words_in_line = map(lambda x: x in line, exclusion_list)
                                  if any(inclusion_words_in_line) and not any(exclusion_words_in_line): #Main argument which includes the search word and excludes the exclusion words

                                      fo.write(line)  #writes in the output file
                                      found_s = True

                               elif user_input1 == None and user_input2 != None: #This portion executes if no exclude word is given,only the search word    
                                   if any(inclusion_words_in_line):
                                       fo.write(line)
                                       found_e = True
                                       found_s = True
                                       found_e1 = False

                       if user_input2 == None and user_input1 != None:       #No search word entered   

                           print("No search word entered.")

                       if not found_s and found_e:             #If the search word is not found                        
                           print("The search word couldn't be found.")
                           fo.close()
                           os.remove(output_file)

                       elif not found_e and not found_s:      #If both are not found                        
                           print("\nNOTE: \nCopy error.")
                           fo.close()
                           os.remove(output_file)

                       elif not found_e1:               #If only the search word is entered                              
                           print("\nNOTE: \nThe exclusion word was not entered! \nWriting only the lines containing search words")

          except IOError:
              print("IO error or wrong file name.")
              fo.close()
              os.remove(output_file)
  if user_input1 != user_input2 :  #this part prevents the output file creation if someone inputs 2 same words creating an anomaly.
         include_exclude(args.input_file, args.output_file, user_input1, user_input2);


  if user_input1 == user_input2 :  #This part prevents the program from running further if both of the words are same
         sys.exit('\nERROR!!\nThe word to be excluded and the word to be included cannot be the same.') 


 except SystemExit as e:                       #Exception handles sys.exit()
       sys.exit(e)



if __name__ == '__main__':
  main()
Was it helpful?

Solution

The typical way to do this is to pick one case, and make all comparisons in that:

if word.lower() == "exception":

For your case, this could look like:

inclusion_words_in_line = map(lambda x: x in line.lower(), 
                              inclusion_list)

OTHER TIPS

this looks like an attempt to build a search engine, you can achieve this using a library like pylucene

you can then be able to run queries like:

+include -exclude

well, and of course many many more, it may worth the learning curve.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top