我想消除文件中的一行,但是当我执行line.replace函数时,它将替换不应替换的内容。
我尝试使用line.replace删除“ SCOPE1EXPRESSION”行,但它删除了我想保留的其他行。
输入文件如下:
SCOPE1EXPRESSION
InDifferentialPairClass'DIFF100'
TOPLAYER_PREFGAP
6.2992mil
BOTTOMLAYER_PREFGAP
6.2992mil
SCOPE1EXPRESSION
InDifferentialPairClass'DIFF90'
TOPLAYER_PREFGAP
4.4882mil
BOTTOMLAYER_PREFGAP
4.4882mil
然后我尝试使用line.replace(“ SCOPE1EXPRESSION”,“”)将其删除:
'''
infile = open(('All_Rules1.txt'),'r')
outfile = open('All_Rules2.txt','w+')
for line in infile:
line = line.replace("SCOPE1EXPRESSION","")
if pattern.search(line):
outfile.write(line)
infile.close()
outfile.close()
sys.stdout.flush()
'''
结果是:
InDifferentialPairClass'DIFF100'
TOPLAYER_PREFGAP
BOTTOMLAYER_PREFGAP
InDifferentialPairClass'DIFF90'
TOPLAYER_PREFGAP
BOTTOMLAYER_PREFGAP
如何防止它删除多余的行?
我希望仅 删除“ SCOPE1EXPRESSION”。