[英]How to count words (and also those with accents !) in a text file in Python?
[英]Python - count the number of words in text file that are also in csv word list
我有這個代碼,它計算“想要”中的單詞出現在文本文件中的次數。
import re
import collections
from collections import Counter
wanted = "fish chips steak"
cnt = Counter()
words = re.findall('\w+', open('textFile.txt').read().lower())
for word in words:
if word in wanted:
cnt[word] += 1
print cnt
我希望能夠計算.txt文件中csv文件中的單詞數,而不是自己給出單詞。
import re
import collections
from collections import Counter
wanted = "CsvFileWithWords.csv"
cnt = Counter()
words = re.findall('\w+', open('textFile.txt').read().lower())
for word in words:
if word in wanted:
cnt[word] += 1
print cnt
我怎樣才能做到這一點?
那這個呢:
csv=open("csvfile").read()
wordlist=csv.split(";")
for i in wordlist:
wanted +=" "+i
閱讀CsvFileWithWords.csv並將其單詞放在列表中。 你可以做同樣的事情來獲取textFile.txt中的單詞。
wanted = re.findall('\w+', open('CsvFileWithWords.csv').read().lower())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.