Advertisement
luisnaranjo733

Morse code

Feb 19th, 2012
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import pickle
  4.  
  5. message = ".... . .-.. .-.. --- / -.. .- .. .-.. -.-- / .--. .-. --- --. .-. .- -- -- . .-. / --. --- --- -.. / .-.. ..- -.-. -.- / --- -. / - .... . / -.-. .... .- .-.. .-.. . -. --. . ... / - --- -.. .- -.--"
  6.  
  7. target = open('pickled_morse.p','r+')
  8.  
  9. "pickled_morse.p is a serialized dictionary of a morse code alphabet, located at http://pastebin.com/mAkTxPRD"
  10.  
  11. morse = dict((v,k) for k, v in pickle.load(target).iteritems()) #{".-":"A"}
  12. target.close()
  13.  
  14. def morse_to_string(msg):
  15.     msg = msg.split()
  16.     word = ''
  17.     for item in msg:
  18.         if item == "/":
  19.             word += " "
  20.             continue
  21.         try:
  22.             word += morse[item]
  23.         except:
  24.             print "Error - could not translate:\t%s" % item
  25.     return word
  26.  
  27. if __name__ == "__main__":
  28.     print morse_to_string(message)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement