I am trying to change a some lines in a text file without affecting the other lines. This is what\'s inside the text file called \"text.txt\"
this is a test
try this solution
with open('test', inplace=True) as text_file: for line in text_file: if line.rsplit('|', 1)[-1].strip() == 'number3': print '{}|{} \n'.format('this is replacement', 'number7') else: print line