首页 > Script > 编码和解码莫尔斯电码的脚本

编码和解码莫尔斯电码的脚本

[总点击:325次]
2009年11月4日

RT
中文先将其编码成 urlencode 然后才转换成莫尔斯电码,呵呵,脚本有两个参数,-e 和 -d
-e 是将文本转换成莫尔斯电码, -d 是将电码转回文本,下面是一个例子:

1
2
3
4
5
xiooli@XIOOLI> python morse.py -e "hello 大家好,我是 xiooli!"                             /tmp
.... . .-.. .-.. --- .-.- ..--- ----- .-.- . ..... .-.- .- ....- .-.- .- --... .-.- . ..... .-.- .- . .-.- -... -.... .-.- . ..... .-.- .- ..... .-.- -... -.. .-.- . ..-. .-.- -... -.-. .-.- ---.. -.-. .-.- . -.... .-.- ---.. ---.. .-.- ----. .---- .-.- . -.... .-.- ----. ---.. .-.- .- ..-. .-.- ..--- ----- -..- .. --- --- .-.. .. .-.- . ..-. .-.- -... -.-. .-.- ---.. .----
xiooli@XIOOLI> python morse.py -d ".... . .-.. .-.. --- .-.- ..--- ----- .-.- . ..... .-.- .- ....- .-.- .- --... .-.- . ..... .-.- .- . .-.- -... -.... .-.- . ..... .-.- .- ..... .-.- -... -.. .-.- . ..-. .-.- -... -.-. .-.- ---.. -.-. .-.- . -.... .-.- ---.. ---.. .-.- ----. .---- .-.- . -.... .-.- ----. ---.. .-.- .- ..-. .-.- ..--- ----- -..- .. --- --- .-.. .. .-.- . ..-. .-.- -... -.-. .-.- ---.. .----"
hello 大家好,我是 xiooli!
xiooli@XIOOLI>                                                                              /tmp

大家可以用这个给小mm写情书哦^^

源代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Name:     morse.py
# Author:   xiooli
#           Email:  <xioooli[at]yahoo.com.cn>
#           Site:   http://joolix.com
# Licence:  GPLv3
# Version:  091104
 
''' Generate and transelate the morse code string. 
'''
import re, urllib, sys
 
# the morse code for % is actually the code for ä or æ, I just borrowed it.
txt_morse={'a':'.-', 'b':'-...', 'c':'-.-.', 'd':'-..',
        'e':'.', 'f':'..-.', 'g':'--.', 'h':'....',
        'i':'..', 'j':'.---', 'k':'-.-', 'l':'.-..',
        'm':'--', 'n':'-.', 'o':'---', 'p':'.--.',
        'q':'--.-', 'r':'.-.', 's':'...', 't':'-',
        'u':'..-', 'v':'...-', 'w':'.--', 'x':'-..-',
        'y':'-.--', 'z':'--..', '0':'-----', '1':'.----',
        '2':'..---', '3':'...--', '4':'....-', '5':'.....',
        '6':'-....', '7':'--...', '8':'---..', '9':'----.',
        '.':'.-.-.-', ',':'--..--', ':':'---...', '?':'..--..',
        '-':'-....-', '/':'-..-.', '=':'-...-', '@': '...-.-',
        '!':'...-.-', '\n':'', '%':'.-.-', ',':'--..--',
        ';':'-.-.-.', '_':'..--.-', '"':'.-..-.', '(':'-.--.',
        ')':'-.--.-', '$':'...-..-', ' ':' '
        }
morse_txt = {}
for i,j in txt_morse.items():
    morse_txt[j] = i
 
def gen_mc(str):
    mc = []
    str = urllib.quote(str).lower()
 
    for c in list(str):
        mc.append(txt_morse[c]+ " ")
    return "".join(mc)
 
def decode_mc(str):
    L = []
    l = str.split(" ")
 
    for mc in l:
        L.append(morse_txt[mc])
    str = "".join(L)
    return urllib.unquote(str)
 
if __name__ == "__main__":
    if len(sys.argv) <= 2:
        print "Error, at lest 2 arguments is needed!"
        sys.exit(1)
    elif sys.argv[1] == "-e":
        try:
            print gen_mc(sys.argv[2])
        except:
            print "Morse encoding failed, please check your string."
    elif sys.argv[1] == "-d":
        try:
            print decode_mc(sys.argv[2])
        except:
            print "Morse decoding failed, please check your string."
    else:
        print "Argument not recorded, don't know how to handle!"
        sys.exit(2)

Script

本文的评论功能被关闭了.