Skip to content

Instantly share code, notes, and snippets.

@HauptJ
Created June 26, 2020 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HauptJ/7c38a82b07f3ae5bc06206496a59032b to your computer and use it in GitHub Desktop.
Save HauptJ/7c38a82b07f3ae5bc06206496a59032b to your computer and use it in GitHub Desktop.
Python Word Count using Dictionary
"""
Find repeating words within text file
return:
word and count
Sentence is here. Sentence two is here.
"""
def main()
try:
textFile = "test.txt"
fs = open(test.txt)
fileText = fs
wordDict = {}
# strip periods
fileText.strip(".", "")
# load hash table words split by space delimiter
wordList = fileText.split("")
for word in wordList:
if wordDict[word]:
wordDict[word] +=1
else:
wordDict[word] = 1
for each k, v in wordDict:
print("word: " + k + " count: " + v)
catch IOExption e:
print("Failed to read file: "+ str(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment