-3

I have text file, I am searching for specific word "Belief". I want the word to be shown in red colour if it was found.

searchfile = open("demo.txt", "r")
text=input("Enter search word :")
for line in searchfile:
    if text in line:
        print(line)
searchfile.close()
linusg
  • 6,289
  • 4
  • 28
  • 78
SMO
  • 43
  • 5

1 Answers1

0

You can get colors as shown here

If you are not using Windows you could try termcolor to do something like this:

from termcolor import colored

text=input("Enter search word :")
with open("demo.txt", "r") as searchfile:
    for line in searchfile:
        if text in line:
            print(colored(text,'red').join(line.split(text)))

Example:

s = "123 321 123 321 123"
print(colored("321",'red').join(s.split("321")))

With output: enter image description here

If you do use windows, you could still run the same code as above, as long as you add the following two lines at the beginning of your script:

from colorama import init
init()

Both libraries are pip-installable and lightweight.

Community
  • 1
  • 1
M.T
  • 4,917
  • 4
  • 33
  • 52
  • from colorama import init init() s="123 321 123 321" print(colored("321",'red').join(s.split("321"))) – SMO Apr 17 '16 at 11:47