我的代码可以工作,但问题是在错误的地方替换。你知道吗
有人能看看我的代码吗。你知道吗
def find():
openfile = open(filename, "rt")
closefile = open(filename, "wt")
inp1 = input(search)
inp2 = input(replace)
for line in fileopen:
newword = fileout.write(line.replace(inp1, inp2))
openfile.close()
closefile.close()
return newword
find()
正如@splash58所说,简单的方法是在单词周围加空格:
更好的方法是使用regex,意思是在搜索时添加单词边界(
\b
)你可以用regex来做这个!正则表达式允许您搜索单词边界以及特定的子字符串。您可以为此使用
\b
标识符。这样你就可以确定你只学到了完整的单词,而不是当它是另一个单词的一部分时。你知道吗相关问题 更多 >
编程相关推荐