Skip to content

Instantly share code, notes, and snippets.

@Pythonian
Created May 28, 2022 13:28
Show Gist options
  • Save Pythonian/1ccb75bccec9615e5b3ed6895078e178 to your computer and use it in GitHub Desktop.
Save Pythonian/1ccb75bccec9615e5b3ed6895078e178 to your computer and use it in GitHub Desktop.
import string
def read_file_content(filename):
with open(filename, 'r') as f:
text = f.read()
return text
def count_words():
text = read_file_content("./story.txt")
words = []
d = {}
for line in text:
line = text.rstrip()
line = text.translate(
line.maketrans("", "", string.punctuation))
words = line.split(" ")
for word in words:
d[word] = 1 if not word in d else d[word] + 1
print(d)
if __name__ == '__main__':
count_words()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment