[英]Remove large textblock over multiple lines in python
我有一个很大的很大的文本文件,需要删除某个文本块。 我要删除的文本在第452行开始,在第470行结束,看起来像这样:
<ItemGroup>
<None />
<None Include="" />
<None Include="" />
<None Include="">
<LogicalName>Info.plist</LogicalName>
</None>
<None Include="" />
<None Include="" />
<None Include=" ">
<LogicalName>Info.plist</LogicalName>
</None>
<None Include="" />
<None Include="">
<LogicalName>Info.plist</LogicalName>
</None>
<None Include="Entitlements.plist" />
</ItemGroup>
我如何通过我的python脚本删除它? 我试图做的是以下没有成功的事情:
with open(os.path.join('folder', 'text.txt'),'r') as file:
content= file.readlines()
numb = 451
toadd=content[451:471]
for i in range(len(toadd)):
toadd[i] = toadd[i].replace(toadd[numb], '')
numb = numb +1
contout = content - toadd
with open(os.path.join('folder', 'text.txt'),'w') as out:
out.writelines(contout)
在这里,我尝试替换行452和470内的每一行,但它不会更改文件中的任何内容。
如何删除文本文件中间的大文本块(彼此之间大约相隔20行)?
尝试执行此操作以删除您的文本块。 如果行号与索引不匹配,则可能需要调整数字。
with open(os.path.join('folder', 'text.txt'),'r') as file:
content = file.readlines()
new_content = content[0:452] + content[471:]
with open(os.path.join('folder', 'new_text.txt'),'w+') as out:
out.writelines(new_content)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.