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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import time
text_to_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': '----.', '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': '--..', '.': '.-.-.-', ',': '--..--', '?': '..--..',
'\'': '.----.', '!': '-.-.--', '/': '-..-.', '(': '-.--.', ')': '-.--.-',
'&': '.-...', ':': '---...', ';': '-.-.-.', '=': '-...-', '+': '.-.-.',
'-': '-....-', '_': '..--.-', '"': '.-..-.', '$': '...-..-', '@': '.--.-.',
' ': '/', '\\': '-..-..'}
# 字符与字符之间用空格连接,单词与单词用“/”区分,输入的文本区分大小写,
# 空格在摩尔斯电码中通常用正斜杠 / 代替。

# 用"滴"表示".",用"答"表示"-",有的地方可能用1和0表示两种状态,在记事本上先替换一下就可以
text_to_dida_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': '答答滴滴', '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': '答答答答滴', '.': '滴答滴答滴答答滴', ',': '答答滴滴答答答答', '?': '滴滴答答滴滴答答',
'!': '答滴答滴答答答答', ':': '答答答滴滴滴答', ';': '答滴答滴答滴答答', '-': '答滴滴滴滴答', '_': '滴滴答答滴答答',
'(': '答滴答答答滴', ')': '答滴答答答滴答', '"': '滴答滴滴答滴滴', '$': '滴滴滴答滴滴答滴', '@': '滴答答滴滴答滴',
'&': '滴答滴滴滴滴滴', '+': '滴答滴答滴滴', '=': '答滴滴滴答', '/': '答滴滴答滴答', '\\': '答滴滴答滴滴',
'#': '滴滴答答答滴滴', ' ': '/'
}

morse_to_text = {} # morse_to_text:定义一个新的字典用来储存反转后的字典
for i, j in text_to_morse.items():
if i in 'abcdefghigklmnopqrstuvwxyz':
i = i.upper()
morse_to_text[j] = i # 反转字典
else:
morse_to_text[j] = i # 反转字典

dida_morse_to_text = {} # dida_morse_to_text:定义一个新的字典用来储存反转后的字典
for i, j in text_to_dida_morse.items():
if i in 'abcdefghigklmnopqrstuvwxyz':
i = i.upper()
dida_morse_to_text[j] = i # 反转字典
else:
dida_morse_to_text[j] = i # 反转字典


# 文本化为摩尔斯电码
def Text_to_morse(text):
# 定义一个可以将文本转换成摩尔斯电码的函数
morse_code = "" # morse_code用来储存摩尔斯电码
for i in text:
if i in text_to_morse.keys():
morse_code = morse_code + text_to_morse[i] + ' '
else:
morse_code = morse_code + '' # 如遇其他陌生字符省略
return morse_code


# 文本化为dida摩尔斯电码
def Text_to_dida_morse(text):
# 定义一个可以将文本转换成摩尔斯电码的函数
dida_morse_code = "" # morse_code用来储存摩尔斯电码
for i in text:
if i in text_to_dida_morse.keys():
dida_morse_code = dida_morse_code + text_to_dida_morse[i] + ' '
else:
dida_morse_code = dida_morse_code + '' # 如遇其他陌生字符省略
return dida_morse_code


# 摩尔斯电码化为文本
def Morse_to_text(morse):
# 定义一个可以将摩尔斯电码转换成文本的函数
text = "" # text用来储存文本
for i in morse:
if i in Morse_to_text.keys():
text = text + Morse_to_text[i] + ' '
else:
text = text + '' # 如遇其他陌生字符省略
return text


# dida摩尔斯电码化为文本
def Dida_morse_to_text(dida_morse):
# 定义一个可以将摩尔斯电码转换成文本的函数
text = "" # text用来储存文本
for i in dida_morse:
if i in dida_morse_to_text.keys():
text = text + dida_morse_to_text[i] + ' '
else:
text = text + '' # 如遇其他陌生字符省略
return text


# 摩尔斯电码化为dida摩尔斯电码
def Morse_to_dida_morse(morse):
text = Morse_to_text(morse)

dida_morse = Text_to_dida_morse(text)
return dida_morse


# dida摩尔斯电码化为摩尔斯电码
def Dida_morse_to_morse(dida_morse):
text = Dida_morse_to_text(dida_morse)

morse = Text_to_morse(text)
return morse


while True:
print('输入1:表示将文本转换为摩尔斯电码')
print('输入2:表示将文本转换为摩尔斯拟声电码(滴答)')
print('输入3:表示将摩尔斯电码转换为文本')
print('输入4:表示将摩尔斯拟声电码(滴答)转换为文本')
print('输入5:表示将摩尔斯电码转换为摩尔斯拟声电码(滴答)')
print('输入6:表示将摩尔斯拟声电码(滴答)转换为摩尔斯电码')
print('输入其他字符:程序结束运行!')
x = input("请输入你的选择")
"""你这里输入x默认是字符串或者字符"""
if x =='1':
s = input("")
m = Text_to_morse(s)
print(m)
time.sleep(4)# time.sleep(4)就是停留4秒
elif x == '2':
s = input("")
dm = Text_to_dida_morse(s)
print(dm)
time.sleep(4)
elif x == '3':
s = input("")
t = Morse_to_text(s)
print(t)
time.sleep(4)
elif x == '4':
s = input("")
t = Dida_morse_to_text(s)
print(t)
time.sleep(4)

elif x == '5':
s = input("")
dm = Morse_to_dida_morse(s)
print(dm)
time.sleep(4)
elif x == '6':
s = input("")
m = Dida_morse_to_morse(s)
print(m)
time.sleep(4)
else:
break
print("欢迎美女再次光临寒舍")

上面就是摩斯电码的python转换系统