Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3.2
- # -*- coding: utf-8 -*-
- # main.py
- """ Task: Exercise 9.1
- Write a function that reads the words in words.txt and stores them as keys
- in a dictionary. It doesn’t matter what the values are. Then you can use
- the in operator as a fast way to check whether a string is in the
- dictionary.
- """
- # words.txt content:
- """
- Write a function that reads the words in words.txt and stores them as keys
- in a dictionary. It doesn’t matter what the values are. Then you can use
- the in operator as a fast way to check whether a string is in the
- dictionary.
- words.txt
- words.
- EOF
- """
- ''' Main '''
- en_de = dict() # empty dictionary (English - German)
- strips = ['.', ':', ',', ';', '!', '?'] # for example
- try:
- fhand = open('words.txt')
- except IOError:
- print("No such file available!")
- exit()
- for line in fhand:
- words = line.split()
- for i in words:
- word = i
- for item in strips:
- word = word.strip(item)
- if len(words) > 0 and word not in en_de:
- en_de[word] = '' # TODO: translate ;)
- print(en_de)
- try:
- fhand.close()
- except:
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement