Using Python, I need to search a text file for a string “that/”
I then need get the line number (currently doing so by using enumerate), and print off
“that\” + the remaining text until the next space.
Example text: First/LS thing/NN I/PRP want/VBP to/TO ask/VB is/VBZ if/IN you/PRP remember/VBP any/DT books/NNS that/IN you/PRP read/VBP as/IN a/DT child/NN
Example output: that/IN 14
Here is the code I have right now, which functions correctly, but wherever it should print “that/xx” it prints nothing.
with open(filename) as f:
for num, line in enumerate(f, 1):
if 'that/' in line:
myString = line
mySub = myString[myString.find('that/'):myString.find(' ')]
print(mySub, str(num))
formattedLines.append(mySub + ' ' + str(num) + '\n')
I think the problem is that the whitespace can happen before the
that/. Start your search for the whitespace at the index wherethat/was found: