使用continue跳過本次寫循環就可以了
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#文本內容 Yesterday when I was young 昨日當我年少輕狂 The tasting of life was sweet 生命的滋味是甜的 As rain upon my tongue tasting I lived by night and shunned the naked light of day tasting123 And only now I see how the time ran away tasting tasting |
將文本中的 tasting123刪除
1
2
3
4
5
6
7
8
|
with open("fileread.txt","r",encoding="utf-8") as f: lines = f.readlines() #print(lines) with open("fileread.txt","w",encoding="utf-8") as f_w: for line in lines: if "tasting123" in line: continue f_w.write(line) |
以上這篇python 文件操作刪除某行的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/bigberg/p/6495373.html