摩尔斯电码转换python123
时间: 2023-07-28 21:53:39 浏览: 104
以下是将摩尔斯电码转换成文本的 Python 代码:
```python
morse_code = {
'.-': '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', '..---': '2', '...--': '3',
'....-': '4', '.....': '5', '-....': '6', '--...': '7',
'---..': '8', '----.': '9', '-----': '0', '--..--': ', ',
'.-.-.-': '.', '..--..': '?', '-..-.': '/', '-....-': '-',
'-.--.': '(', '-.--.-': ')', '.-...': '&', '---...': ':',
'-.-.-.': ';', '-...-': '=', '.-.-.': '+', '-....-': '-',
'..--.-': '_', '.--.-.': '@', '...-..-': '$', '.----.': '\'',
'-...-': '='
}
def morse_to_text(morse_code):
words = morse_code.split(' / ')
text = []
for word in words:
letters = word.split(' ')
for letter in letters:
text.append(morse_code[letter])
text.append(' ')
return ''.join(text[:-1])
# 示例
morse = '-.--. .-.. .- ... / .. ... / ..-. --- .-. / --. ..- -.-- ...'
print(morse_to_text(morse)) # 输出 'PLAY IS FOR GUS'
```
以上代码将摩尔斯电码转换成英文文本。如果需要将文本转换成摩尔斯电码,则需要编写相应的代码来进行转换。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)