I have a csv file such as the following in one column. The symbols and numbers are only to show that the file does not just contain text. I have two objectives:
- count the number of occurrences of a word;
- determine how many rows a word appears in.
Stuff
I like apples. Sally likes apples.
Jim has 4 berries. !@#
John has 2 apples.
Ideally, the code should return something like: {apples: 3} {# of rows: 2}
I've written some code to try and count occurrences, but it isn't running properly (assumedly because of the punctuation). Also, I do not know how to determine the number of rows a word appears in; this could be as simple as counting the number of unique occurrences in each row, but I'm unsure of how to proceed. Here is the code I have so far, done in Python 3.6.1:
import csv
my_reader = csv.reader(open('file.csv', encoding = 'utf-8'))
ctr = 0
for record in my_reader:if record[0] == 'apples':ctr += 1
print(ctr)
The code merely returns 0
as the answer. Help?