I have this code which counts how many times the words from "wanted" appear on text file..
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
I would like instead of giving the words myself, to be able to counting the number of words in the .txt file that are in a csv file
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
How can I do that?
What about this:
csv=open("csvfile").read()
wordlist=csv.split(";")
for i in wordlist:
wanted +=" "+i
Read CsvFileWithWords.csv and put its words in a list. You can do the same you are doing to get the words in textFile.txt.
wanted = re.findall('\w+', open('CsvFileWithWords.csv').read().lower())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.