Advertisement
Mars83

9-1

Oct 9th, 2011
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. #! /usr/bin/env python3.2
  2. # -*- coding: utf-8 -*-
  3.  
  4. # main.py
  5. """ Task: Exercise 9.1
  6.    Write a function that reads the words in words.txt and stores them as keys
  7.    in a dictionary. It doesn’t matter what the values are. Then you can use
  8.    the in operator as a fast way to check whether a string is in the
  9.    dictionary.
  10. """
  11.  
  12. # words.txt content:
  13. """
  14. Write a function that reads the words in words.txt and stores them as keys
  15. in a dictionary. It doesn’t matter what the values are. Then you can use
  16. the in operator as a fast way to check whether a string is in the
  17. dictionary.
  18.  
  19. words.txt
  20. words.
  21.  
  22. EOF
  23. """
  24.  
  25. ''' Main '''
  26. en_de = dict()      # empty dictionary (English - German)
  27. strips = ['.', ':', ',', ';', '!', '?']     # for example
  28. try:
  29.     fhand = open('words.txt')
  30. except IOError:
  31.     print("No such file available!")
  32.     exit()
  33. for line in fhand:
  34.     words = line.split()
  35.     for i in words:
  36.         word = i
  37.         for item in strips:
  38.             word = word.strip(item)
  39.         if len(words) > 0 and word not in en_de:
  40.             en_de[word] = ''    # TODO: translate ;)
  41. print(en_de)
  42. try:
  43.     fhand.close()
  44. except:
  45.     exit()
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement