Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. import sys
  2.  
  3. def parse_and_count_words():
  4.   with open('text.txt','r') as file:
  5.     text = file.read()
  6.   word_counter = {}
  7.   text = text.strip()
  8.   text = text.lower()
  9.   words = text.split(" ")
  10.  
  11.   for word in words:
  12.     if word in word_counter:
  13.       word_counter[word] = word_counter[word] + 1
  14.     else:
  15.       word_counter[word] = 1
  16.   return word_counter
  17.    
  18. def print_dict(displayed_dict):
  19.   with open('text.txt','w') as file:  
  20.     for word in displayed_dict.keys():
  21.       file.write(('{} = {}\n').format(word,displayed_dict[word]))
  22.  
  23. def main():
  24.  
  25.     if hasattr(sys, 'ps1'):
  26.         print("Running interactively.")
  27.     else:
  28.         print("Not running interactively")
  29.    
  30.     print_dict(parse_and_count_words())
  31.  
  32. if __name__ == "__main__":
  33.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement