new_a="" with open(r'C:\Users\md100\Desktop\liu.txt','r',encoding='utf-8') as fel: # print(a) for i in fel: print(i) if '小兰: 12本'in i: i=i.replace('小兰: 12本','小兰: 14本') new_a+=i print(new_a) with open(r'C:\Users\md100\Desktop\liu.txt','w',encoding='utf-8') as fel: fel.write(new_a)