Fred Bongers Fred Bongers - 4 months ago 9
Python Question

Serach for word in text file in Python

I have this text file:

MemTotal,5,2016-07-30 12:02:33,781
model name,3,2016-07-30 13:37:59,074
model,3,2016-07-30 15:39:59,075


i need to find the line with the model.

My code:

term = "model"
file = open('file.txt')
for line in file:
line.strip().split('/n')
if term in line:
print line
file.close()


This is the output:

model name,3,2016-07-30 13:37:59,074
model,3,2016-07-30 15:39:59,075


i need only this line as output:

model,3,2016-07-30 15:39:59,075


how can i do this?

Answer

Just replace the line:

if term in line:

with line :

if line.startswith('model,'):