修改文件中的某个字段
如:将 “夜晚的” 改为 “白天的寒冷的”
1 Somehow, it seems the love I knew was always the most destructive kind 2 不知为何,我经历的爱情总是最具毁灭性的的那种 3 Yesterday when I was young 4 昨日当我年少轻狂 5 The taste of life was sweet 6 生命的滋味是甜的 7 As rain upon my tongue 8 就如舌尖上的雨露 9 I teased at life as if it were a foolish game 10 我戏弄生命 视其为愚蠢的游戏 11 The way the evening breeze 12 就如夜晚的微风
1 f = open("lyrics","r",encoding="utf-8") 2 f2 = open("lyrics_new","w",encoding="utf-8") 3 4 find_str= "夜晚的" 5 new_str = "白天的寒冷的" 6 for line in f: 7 if find_str in line: 8 line = line.replace(find_str,new_str) 9 f2.write(line) 10 11 f.close() 12 f2.close()