我想找到以列表中的单词开头的行。如果找到这个词,我希望它所在的行和前一行被删除。我能够得到该行和上一个并打印它们,但我无法将它们传递给我的输出文件。铁:
输入:
This is not supposed to be deleted.
This shall be deleted.
Titel
This is not supposed to be deleted.
This is not supposed to be deleted
输出:
This is not supposed to be deleted.
This is not supposed to be deleted.
This is not supposed to be deleted
我用这段代码试过了,但我不断收到 TypeError: 'str' object does not support item assignment
with open(file1) as f_in, open(file2, 'w') as f_out:
lines = f_in.read().splitlines()
for i, line in enumerate(lines):
clean = True
if line.startswith(('Text', 'Titel')):
for (line[i-1]) in lines:
clean = False
for line in lines:
clean =False
if clean == True:
f_out.write(line)