0

我的任务是使用 python 将文本文件中的“O”(大写 O)替换为“0”。但一个条件是我必须保留其他词,如 Over、NATO 等。我只需要替换 9OO 到 900、2OO6 到 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,'"')
4

3 回答 3

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))
于 2013-06-13T21:22:41.820 回答
0

您可能只需匹配一个前导数字后跟O. 这将无法处理OO7,但8080例如可以很好地工作。这里没有匹配尾随数字的答案。如果你想这样做,你需要使用前瞻匹配。

re.sub(r'(\d)(O+)', lambda m: m.groups()[0] + '0'*len(m.groups()[1]), content)
于 2013-06-13T21:21:43.147 回答
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,'"')
于 2013-06-13T21:24:17.123 回答