所以我必須替換給定檔案中的單詞。問題是您可能在其他不應替換的單詞中包含此潛臺詞,因此string.replace()不能在此處使用。這個詞也可能有符號,".,;:!?"
例如在給定的檔案中有Bobtail has a tail.,所有單詞“tail”必須替換為“head”,所以在這種情況下的答案應該是Bobtail has a head.
uj5u.com熱心網友回復:
對您來說可能足夠好的最簡單的解決方案是在您要在.replace()通話中替換的單詞周圍添加一些空格。
使用您的示例:
str = "Bobtail has a tail."
print(str.replace("tail", "nose")) # Bobnose has a nose.
print(str.replace(" tail", "nose")) # Bobtail has a nose.
下一步是使用正則運算式來查找要替換的字串。這有點復雜且特定于案例,因此您可能想要使用RegExr 之類的東西來嘗試構建一個。
uj5u.com熱心網友回復:
對于這種復雜的字串搜索,您必須使用正則運算式。在 python 中,這可以通過匯入 RE 模塊來完成。然后您可以使用該search()方法在字串中查找任何給定的正則運算式。結果可通過該group()方法訪問。鑒于您知道如何回圈檔案的內容,您的解決方案如下所示:
import re
substring = some_file.txt
result =[]
for e in substring:
regexp = re.search("(\s|\.|\,|\?|\!|\:|\;)tail(\s|\.|\,|\?|\!|\:|\;)",e,1)
if regexp.group() is not None:
result.push(e.replace(regexp.group(),"head"))
else:
result.push(e)
您可以在此處使用正則運算式進行更多練習:https : //regexr.com/
為簡單起見,我沒有包括每個特殊字符。請注意,您必須使用反斜杠字符對它們進行轉義。使用\s的空格。
uj5u.com熱心網友回復:
就像 Dan P 提到的,你要找的是 python re模塊,特別是sub方法。
以這個字串為例:
s = "Bobtail has a !!tail.!! and the ..tail> is just a part of Bobtails' body"
使用正則運算式單詞邊界運算子 \b
resulting_string = re.sub('\\btail\\b','head', s)
"Bobtail has a !!head.!! and the ..head> is just a part of Bobtails' body"
要消除特殊字符,您可以在正則運算式模式中使用更復雜的東西,例如:
resulting_string = re.sub('\\W\\S?tail\\S*','head',s)
"Bobtail has a head and the head is just a part of Bobtails' body"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/341238.html
上一篇:迭代文本檔案中的數字
下一篇:洗掉N行檔案
