擅长:python、mysql、java
<p>这不是最好的解决方案,但一定要试一试</p>
<p><code>c.txt</code></p>
<pre><code>Andrew
andrew@email.com
314657463
Ariana
ariana@email.com
1026479657
</code></pre>
<p>编写此代码时假设每第三行就有一个新联系人</p>
<pre><code>x=open('c.txt','r')
test_list=[]
for y in x.readlines():
#print(y.strip("\n"))
test_list.append(y.strip("\n"))
while True:
j=input("Enter a name to delete: ")
if j in test_list:
sd=test_list.index(j)
for i in range(0,3):
po=test_list.pop(sd)
f=open('all.txt',"w")
for j in test_list:
f.write(j+"\n")
print("File written successfully")
break
else:
print("\nPlease enter the correct name...\n")
</code></pre>
<p>运行示例:</p>
<pre><code>Enter a name to delete: Ariana
File written successfully
</code></pre>
<p><code>all.txt</code></p>
<pre><code>Andrew
andrew@email.com
314657463
</code></pre>