在 test.txt 中:
rt : objective
tr350rt : objective
rtrt : objective
@username : objective
@user_1236 : objective
@254test!! : objective
@test : objective
#15 : objective
我的代码:
import re
file3 = 'C://Users/Desktop/test.txt'
rfile3 = open(file3).read()
for altext in rfile3.split("\n"):
saltext = altext.split("\t")
for saltword in saltext:
ssaltword = saltword.split(" ")
if re.search(r'^rt$', ssaltword[0]):
print ssaltword[0], ssaltword[2]
testreplace = open(file3, 'w').write(rfile3.replace(ssaltword[0], ""))
if re.search(r'^@\w', ssaltword[0]):
print ssaltword[0], ssaltword[2]
testreplace = open(file3, 'w').write(rfile3.replace(ssaltword[0], ""))
我有:
: objective
tr350 : objective
: objective
@username : objective
@user_1236 : objective
@254test!! : objective
: objective
#15 : objective
我试图只用空格替换“rt”和所有@
但是从我的代码中,所有的“rt”都被替换了,只有一个@被替换了。
我想得到:
: objective
tr350rt : objective
rtrt : objective
: objective
: objective
: objective
: objective
#15 : objective
有什么建议吗?