Skip to content

Instantly share code, notes, and snippets.

@Pythonian
Created May 28, 2022 13:28

Revisions

  1. Pythonian created this gist May 28, 2022.
    22 changes: 22 additions & 0 deletions count_words.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    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()