Python - 计算文本文件中同时出现在 csv 单词列表中的单词数

Python - count the number of words in text file that are also in csv word list

我有这段代码可以计算 "wanted" 中的单词在文本文件中出现的次数..

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

我希望能够计算 csv 文件中 .txt 文件中的单词数,而不是自己给出单词数

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())