我正在使用Excel数据编辑大型txt文件的一部分。其中一些编辑需要删除某些部分,而其他一些则需要复制,粘贴和稍微编辑这些部分。
例如,txt文档采用以下基本形式:
Monitor 1
Monitor 2
Item1 Item2
End
Monitor 3
Monitor 4
Monitor 5
Item3 Item4
End
Monitor 6
Item4 Item5
End
说我需要删除第二个块(即从第一端之后到监视器6之前的行删除)。我可以使用对Item3和Item4的查找来找到块。有没有办法将此块分配给变量?
with open(longStr1) as old_file:
lines = old_file.readlines()
with open(endfile1, "w") as new_file:
for line in lines:
if "Item3 Item4" in line:
#block = from previous End to after next End
lines.remove(block)
new_file.write(line)
所以理想情况下,这将允许我删除整个文本块以使txt文件看起来像这样:
Monitor 1
Monitor 2
Item1 Item2
End
Monitor 6
Item4 Item5
End
如何使用python定义此块?
答案 0 :(得分:1)
迭代方法:
search_item = "Item3 Item4"
with open('input.txt') as f_in, open('output.txt', 'w') as f_out:
block = ''
for line in f_in:
if block:
block += line
if line.strip() == 'End':
if search_item not in block: f_out.write(block + '\n')
block = ''
elif line.startswith('Monitor'):
block = line
output.txt
内容:
Monitor 1
Monitor 2
Item1 Item2
End
Monitor 6
Item4 Item5
End
答案 1 :(得分:0)
您可以累积这些行,以在与一个块的内容相对应的单独字符串中进行写入。等到到达块的末尾(“结束”行),然后再写入累积的行。如果在此过程中找到指示禁止执行该操作的文本模式,请设置一个标志,以防止在到达末尾时写入文件。
例如(未测试):
with open(longStr1) as old_file:
lines = old_file.readlines()
lineBlock = ""
excluded = False
with open(endfile1, "w") as new_file:
for line in lines:
lineBlock += line
if line == "End":
if not excluded:
new_file.write(lineBlock)
lineBlock = ""
excluded = False
if "Item3 Item4" in line:
excluded = True