I\'m writing a Python script. I need to search a text file for a word and then print part of that line. My problem is that the word will not be an exact match in the text fi
Well, it may not be much, but you could always use regex:
m = re.search(r'(color\=.+?(?= )|color\=.+?$)', line)
if m:
text = m.group() # Matched text here
Here's one way - split each line by spaces, then search each part for "color=":
with open("textfile.txt") as openfile:
for line in openfile:
for part in line.split():
if "color=" in part:
print part