Read/Write text file

后端 未结 5 1754
眼角桃花
眼角桃花 2020-12-20 06:46

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         


        
5条回答
  •  眼角桃花
    2020-12-20 06:54

    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
    

提交回复
热议问题