使用Python 2.7我打開包含外部文件:獨特字數
Text another word1
Lorem ipsem word1 something
hello first word2 post
我只希望,而不是在第三行上的字word
執行獨特的計數。
所需的輸出:
$ script.py
2x word1
1x word2
我得到了這麼遠,但失敗..:
import os
import sys
from collections import Counter
with open('./file.txt', 'r') as file:
for item in file:
if '.sh' in item:
all = item.split()[2]
print Counter(all.split())
你希望所有的文本以'word'和他們算什麼? –