Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- def parse_and_count_words():
- with open('text.txt','r') as file:
- text = file.read()
- word_counter = {}
- text = text.strip()
- text = text.lower()
- words = text.split(" ")
- for word in words:
- if word in word_counter:
- word_counter[word] = word_counter[word] + 1
- else:
- word_counter[word] = 1
- return word_counter
- def print_dict(displayed_dict):
- with open('text.txt','w') as file:
- for word in displayed_dict.keys():
- file.write(('{} = {}\n').format(word,displayed_dict[word]))
- def main():
- if hasattr(sys, 'ps1'):
- print("Running interactively.")
- else:
- print("Not running interactively")
- print_dict(parse_and_count_words())
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement