使用Python,我需要在文本文件中搜索字符串“that/”
然后我需要得到行号(目前是通过使用enumerate实现的),然后打印出来
“that\”+剩余的文本直到下一个空格。在
示例文本:第一个/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
输出示例:that/in14
这是我现在拥有的代码,它可以正常工作,但是无论它在哪里打印“that/xx”,它都不会打印任何内容。在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')