python关于删除特定行的问题

2025-03-09 10:10:03
推荐回答(1个)
回答1:

##No.1
import re
for line in open("a.txt"):
if not re.match("abcd", line):
print line[:-1] #标准输出比较方便

##No.2
omit = False
for line in open("a.txt"):
if line == "#start\n":
omit = True
elif line == "#end\n":
omit = False
else:
if not omit: print line[:-1]