I have a list that returns frequently used words in a .txt
. How can I send those to a CSV (or other Excel file) where one column has the word, and the other has the frequency.
For example, here's the start of my Counter(my_list)
returned values:
Counter({'the': 3317, 'to': 1845, 'and': 1812, 'a': 1580, '': 1248, 'of': 1248, 'Harry': 1213, 'was': 1179, 'he': 1034, 'in': 932, 'his': 895, 'it': 803, 'said': 793, ...
I'd like each word to be in a column, say A, and the count, in B. like
the | 3317
to | 1845
and | 1812
a | 1580
Etc. (Note that it can be sorted alphabetically in the CSV. I'm just trying to get it in there to analyse).
Here's what I have now:
def create_csv(my_list):
with open(r'myCSV.csv', 'w', newline='') as my_CSV:
fieldnames = ['word','count']
writer = csv.writer(my_CSV)
writer.writerow(fieldnames)
for key, value in my_list.items():
writer.writerow(list(key) + [value])
This almost works, except each letter is in a column, followed by the count:
What do I need to change so that the word stays together?
edit: to be sure, here's the function I'm using to create the list. (my_file
is a .txt
file)
def unique_words():
with open(my_file, encoding="utf8") as infile:
for line in infile:
words = line.split()
for word in words:
edited_word = clean_word(word)
lst.append(edited_word)
if edited_word not in lst:
lst.append(edited_word)
lst.sort()
return lst, cnt
and calling it via:
create_csv(Counter(lst))