我有一个这样的文本文件
Name: Python
Address: apple
This is a sample text.
It could start with different text text2.
End
This is where file starts....
What is going on
Yeah
abble
Name: Python
Address: apple
This is a different text.
End
There is another stuff that is written.
What should I do.
This
What is going on
我试图删除Name:和End之间的所有内容,除了保留Name:和End之前的所有内容
import re
with open('testfile.txt') as csvfiles:
data=csvfiles.read()
print (re.sub('Name.*?End','',data, flags=re.DOTALL))
我想要的结果是:
Name: Python
Address: apple
This is a sample text.
It could start with different text text2.
End This is where file starts....
What is going on
Yeah
abble
There is another stuff that is written.
What should I do.
This
What is going on
我得到的是:
This is where file starts....
What is going on
Yeah
abble
There is another stuff that is written.
What should I do.
This
What is going on
如何从Name到End保留第一次出现,并删除Name:到End之间的所有内容
谢谢你, 闪耀
可能不是最快的解决方案,但是可以使用正则表达式来查找模式的所有出现项,并用
''
替换除第一个以外的所有出现项下面是一个与您的模式匹配的正则表达式:^{}
输出:
相关问题 更多 >
编程相关推荐