从文本文件中删除重复部分而不删除第一个引用

2024-04-26 03:30:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个这样的文本文件

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之间的所有内容

谢谢你, 闪耀


Tags: textnameappleisonaddresswiththis
1条回答
网友
1楼 · 发布于 2024-04-26 03:30:43

可能不是最快的解决方案,但是可以使用正则表达式来查找模式的所有出现项,并用''替换除第一个以外的所有出现项

下面是一个与您的模式匹配的正则表达式:^{}

import re

with open('test.txt') as f:
  data = f.read()
  x = re.findall(r'(?m)^(Name[\s\S]*?End|\Z)', data)
  for i in x[1:]:
    data = data.replace(i, '')

  print(data)

输出:

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

相关问题 更多 >