我有一个任务是使用python在文本文件中用“0”替换“O”(大写字母O)。但有一个条件是我必须保留像Over,NATO等其他词。我只需要替换像9OO到900,2006到2006这样的单词等等。我尝试了很多但却没有成功。我的代码如下。请帮助我任何人。提前致谢
import re
srcpatt = 'O'
rplpatt = '0'
cre = re.compile(srcpatt)
with open('myfile.txt', 'r') as file:
content = file.read()
wordlist = re.findall(r'(\d+O|O\d+)',str(content))
print(wordlist)
for word in wordlist:
subcontent = cre.sub(rplpatt, word)
newrep = re.compile(word)
newcontent = newrep.sub(subcontent,content)
with open('myfile.txt', 'w') as file:
file.write(newcontent)
print('"',srcpatt,'" is successfully replaced by "',rplpatt,'"')
答案 0 :(得分:1)
re.sub
可以接受替换功能,所以我们可以很好地削减它:
import re
with open('myfile.txt', 'r') as file:
content = file.read()
with open('myfile.txt', 'w') as file:
file.write(re.sub(r'\d+[\dO]+|[\dO]+\d+', lambda m: m.group().replace('O', '0'), content))
答案 1 :(得分:0)
您可以通过匹配前导数字后跟O
来逃脱。这不会处理OO7
,但它可以很好地与8080
一起使用。这里没有与答案数字匹配的答案。如果你想这样做,你需要使用前瞻性匹配。
re.sub(r'(\d)(O+)', lambda m: m.groups()[0] + '0'*len(m.groups()[1]), content)
答案 2 :(得分:0)
import re
srcpatt = 'O'
rplpatt = '0'
cre = re.compile(srcpatt)
reg = r'\b(\d*)O(O*\d*)\b'
with open('input', 'r') as f:
for line in f:
while re.match(reg,line): line=re.sub(reg, r'\g<1>0\2', line)
print line
print('"',srcpatt,'" is successfully replaced by "',rplpatt,'"')