Reputation: 13
I currently have made a program that takes words from a webpage counts them, then prints the results out with the word and how many times it shows up
ex)
instead of printing the keys and value I want to be able to write them to a text file. currently this is what it looks like
def create_dictionary(clean_word_list):
word_count = {}
for word in clean_word_list:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
for key, value in sorted(word_count.items(), key=operator.itemgetter(1)):
print(key, value)
as you can see at the end its printing the key and value but how to I write that to a txt file instead?
Upvotes: 0
Views: 5612
Reputation: 43504
Another option would be to use the json
module. It wouldn't result in the output format that you posted, but it has the benefit of being easily readable back into a dictionary.
For instance:
import json
word_count = {
'the': 4,
'hello': 10,
'am': 12
}
open("output.txt", "w").write(json.dumps(word_count))
The contents of the file would be:
{
"the": 4,
"am": 12,
"hello": 10
}
But you can read this file back into a dictionary using json.loads()
:
word_counts_dict_from_file = json.loads(open('output.txt', 'r').read())
Upvotes: 0
Reputation: 51673
For counting the collections.Counter
specialized dict is the right tool, simply stuff your list in it and it counts it for you.
If writing to files use the with open(filename, "w") as fileHandle:
- approach that autocloses the filehandle when you leave the scope.
Combined:
from collections import Counter
text = """ No idea what kind of text you got. But sometinmes it is usefull to know about
collections.Counter if you want to count things. Now some repetetive stuff:
Abba BBa Abba BBa Abba Abba Abba BBa BBa Cba"""
# split text into lines at '\n', split the lines into words at ' ' and cleanup whitespace
splitText = [x.strip() for y in text.split('\n') for x in y.split(' ')
if len(x.strip()) > 0]
# 1-line count the words:
c = Counter(splitText)
# output to file
with open("myfile.txt","w") as f:
for i in c:
f.write(f"{i},{str(c[i])}\n")
# print(f"{i},{str(c[i])}") # output to console
Counter has convenience functions to output f.e. the top 5 counted words:
print(c.most_common(3)) # gimme top 3 counted things as list of tuples (word,count)
Output: [('Abba', 5), ('BBa', 4), ('you', 2)]
Doku:
Output-File:
No,1
idea,1
what,1
kind,1
of,1
text,1
you,2
got.,1
But,1
sometinmes,1
it,1
is,1
usefull,1
to,2
know,1
about,1
collections.Counter,1
if,1
want,1
count,1
things.,1
Now,1
some,1
repetetive,1
stuff:,1
Abba,5
BBa,4
Cba,1
Upvotes: 2
Reputation: 4937
You can easily just add file output to your existing code by: 1. Opening a file for writing 2. Telling the print function to write to that file
def create_dictionary(clean_word_list):
word_count = {}
for word in clean_word_list:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
with open('output.txt', 'w') as output:
for key, value in sorted(word_count.items(), key=operator.itemgetter(1)):
print(key, value, file=output)
The with
statement provides context management of the file object and is considered a best practice. If anything raises an exception within the scope of the with
statement, the file will automatically be closed. The same is true when you exit the with
block scope naturally (e.g. you've iterated across all items in the dict).
Upvotes: 0
Reputation: 260
For example:
def create_dictionary(clean_word_list):
word_count = {}
f = open("filename.txt", "w")
for word in clean_word_list:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
for key, value in sorted(word_count.items(), key=operator.itemgetter(1)):
print(key, value)
f.write('{} {}'.format(key, value))
f.close()
Format the output in f.write
part as you wish.
Upvotes: 3