我在 invalid_change 变量中有一个 id,我试图从输入文件“list.txt”中删除它并创建一个如下所示的文件 results.txt..谁能提供有关如何修复此问题的输入?我有一个示例输入和下面的预期输出..
'''
INPUT(list.txt:-
350882 348521 350166
346917 352470
360049
EXPECTEDOUTPUT(results.txt):-
350882 348521 350166
346917
360049
'''
invalid_list=['352470','12345']
f_write = open('results.txt', 'wb')
with open('list.txt','r') as f :
for line in f :
#delete the whole line if any invalid gerrit is presnet
gerrit_list = line.strip().split(' ')
ifvalid = True
for gerrit in gerrit_list:
try: # check if invalid gerrit is present
invalid_gerrit.index(invalid_change)
ifvalid = False
break
except:
pass
if ifvalid:
f_write.write(line)
f_write.close()