所以我想删除文本文件中的一些信息,例如:这是
之前的文本kaspersen@outlook.com| Kristoffer Kaspersen| Cypernsvej| 30, 1| 2300 Kobenhavn| Denmark| 30935414
andrew@duesbury.ca| Andrew Duesbury| 1545 Portsmouth Pl| Mississauga ON L5M 7W1| Canada| +16478975695
won@cloudzndirt.com| Won Oh| 11149 Camarena Ave| MONTCLAIR CA 91763| United States| 9999999999
在
之后kaspersen@outlook.com| Kristoffer Kaspersen| Denmark| 30935414
andrew@duesbury.ca| Andrew Duesbury| Canada| +16478975695
won@cloudzndirt.com| Won Oh|United States| 99999999999
基本上就像删除第二个'|'和第四个'|'之间的信息
这是我以
开头的代码f = open("Extracts.txt", "r")
x = f.readline()
print(x)
a=0
for i in x:
if i =="|":
a+=1
if a ==2:
在这段代码中,我试图计数|它确实工作,但我不太确定如何告诉程序删除,直到到达第4 |。
一种可能性是使用split
:
with open("Extracts.txt", "r") as f, open("output.txt", "w") as g:
for line in f:
fields = line.split('|')
print(*fields[0:2], *fields[-2:], sep='|', end='', file=g)
output.txt
:
kaspersen@outlook.com| Kristoffer Kaspersen| Denmark| 30935414
andrew@duesbury.ca| Andrew Duesbury| Canada| +16478975695
won@cloudzndirt.com| Won Oh| United States| 9999999999