让我们说我想要获得次数"""出现在文本文件中。我如何得到""独自一人:
没有用' '作为子串,如:
- "有",
- "他们",
- "他们的"等
答案 0 :(得分:1)
word = 'the'
reg = re.compile(r'\b%s\b' % word, re.I)
with open('textfile.containing.the','r') as fin:
print(len(reg.findall(fin.read()))
希望有所帮助..
答案 1 :(得分:1)
答案 2 :(得分:1)
使用re.search和计数器。
import re
count =0
with open("test.txt") as f:
for line in f:
if re.search(r"\bthe$",line): # if search does not find a match it will return None
count+=1