2012-12-21 186 views
2

在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 

什么建议吗?

回答

2

我觉得正则表达式是矫枉过正这里:

with open("test.txt") as in_fp, open("test2.txt", "w") as out_fp: 
    for line in in_fp: 
     ls = line.split() 
     if ls and (ls[0].startswith("@") or ls[0] == "rt"): 
      line = line.replace(ls[0], "", 1) 
     out_fp.write(line) 

产生

localhost-2:coding $ cat test2.txt 
: objective 
tr350rt : objective 
rtrt : objective 
: objective 
: objective 
: objective 
: objective 
#15 : objective 

请注意,我也改变了它不覆盖原来的。

编辑:如果你真的想覆盖就地原,然后我读了整个事情到内存第一:

with open("test.txt") as fp: 
    lines = fp.readlines() 

with open("test.txt", "w") as out_fp: 
    for line in lines: 
     ls = line.split() 
     if ls and (ls[0].startswith("@") or ls[0] == "rt"): 
      line = line.replace(ls[0], "", 1) 
     out_fp.write(line) 
+0

对不起,我不熟悉“with open”。我必须覆盖原始文件,然后使用“in_fp.write “),对吗? – ThanaDaray

+1

请考虑让帝斯曼的建议不要覆盖原始文件;这几乎总是可取的。一旦你确定你的代码正在工作,你可以在最后添加一点删除原来的文件并重命名新的匹配旧名称。 'with open'语法只是意味着Python会打开该文件,但只会在下面的范围内保持打开状态。只要代码缩进),然后自动关闭它。 –

+0

@DSM非常感谢。 – ThanaDaray

1
import re 
with open("test.txt") as infile: 
    text = infile.read() 
    newtext = re.sub(r"(?m)^(?:rt\b|@\w+)(?=\s*:)", " ", text) 

说明:

(?m)  # Turn on multiline mode 
^   # Match start of line 
(?:  # Either match... 
rt\b  # rt (as a complete word 
|   # or 
@\w+  # @ followed by an alphanumeric "word" 
)   # End of alternation 
(?=\s*:) # Assert that a colon follows (after optional whitespace) 
+0

我试过了,没有改变。 – ThanaDaray

+0

@ThanaDaray:你看过'newtext'吗?你需要把它写入你的(新的)文件中:'open(“newfile.txt”,“w”outfile:outfile.write(newtext)' –

+0

+1正则表达式解释 – naiquevin

1

试试这个,

import os 

mydict = {"@":'',"rt":''} 

filepath = 'C://Users/Desktop/test.txt' 
s = open(filepath).read() 
for k, v in mydict.iteritems(): 
    s = s.replace(k, v) 
f = open(filepath, 'w') 
f.write(s) 
f.close() 
+1

'import ''语句在这里?你没有使用'walk'或任何其他'os'函数 –

+1

我喜欢这种模式,但我认为它不适用于OP的情况,因为我们不是简单地删除'@'符号,但以@开头的字 – DSM

+0

@KyleStrand谢谢,更新 –

1

甚至没有必要在这里使用正则表达式:

with open("test.txt") as file: 
    lines = file.readlines() 
    for line in lines: 
     if (line.startswith("@") and ":" in line) or line.startswith("rt :"): 
      line = " :" + line.split(":", 1)[1] 
+1

正则表达式解决方案很少是更好的解决方案。 – mmgp

+0

原始问题比您处理的简单“rt:”情况具有更复杂的删除“rt”实例。 –

+0

根据他在OP上的想要的输出,这工作正常。阅读OP上的“我想获得:”部分。 – 2012-12-21 16:25:24