Created
May 28, 2022 13:28
-
-
Save Pythonian/1ccb75bccec9615e5b3ed6895078e178 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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