我有以下脚本,它根据数组标识我要删除的文件中的行但不删除它们.
我应该改变什么?
sourcefile = "C:\\Python25\\PC_New.txt"
filename2 = "C:\\Python25\\PC_reduced.txt"
offending = ["Exception","Integer","RuntimeException"]
def fixup( filename ):
print "fixup ", filename
fin = open( filename )
fout = open( filename2 , "w")
for line in fin.readlines():
for item in offending:
print "got one",line
line = line.replace( item, "MUST DELETE" )
line=line.strip()
fout.write(line)
fin.close()
fout.close()
fixup(sourcefile)
Run Code Online (Sandbox Code Playgroud)
sourcefile = "C:\\Python25\\PC_New.txt"
filename2 = "C:\\Python25\\PC_reduced.txt"
offending = ["Exception","Integer","RuntimeException"]
def fixup( filename ):
fin = open( filename )
fout = open( filename2 , "w")
for line in fin:
if True in [item in line for item in offending]:
continue
fout.write(line)
fin.close()
fout.close()
fixup(sourcefile)
Run Code Online (Sandbox Code Playgroud)
编辑:甚至更好:
for line in fin:
if not True in [item in line for item in offending]:
fout.write(line)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7507 次 |
| 最近记录: |