在Python中,如何读取输入文件,然后将其转换为morse代码?

2024-04-29 05:37:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须创建一个程序,要求用户输入文件,然后创建一个输出文件,其中包含莫尔斯电码编码的信息。当我运行我的程序时,在“translated+=alphabet[words]”行中出现了一个类型错误,它表示这是一个不易损坏的类型:“list”。打开输入文件后,如何将输入文件中的文本翻译成莫尔斯电码?在

函数后面的代码有问题吗?在

inputFileName = input("Enter the input file name:")

outputFileName = input("Enter the output file name:")

def morseCode(inputFileName):
    inputFileName = inputFileName.upper()
    translated = ""
    # Open the input and output files 
    with open(inputFileName) as inputFile, open (outputFileName, "w") as outputFile:
        for line in inputFile:
            words = line.split()
            # Translate letters in dictionary 
            translated += alphabet[line]
            for word in words:
                if word in inputFileName:
                    outputFile.write(inputFile[word])
                else:
                    outputFile.write(word)
                outputFile.write(' ')
            outputFile.write('\n')

            return (outputFile, inputFile, inputFileName, translated)

translated = morseCode(inputFileName)


print(translated)

Tags: 文件thein程序input电码lineword
2条回答

一旦你掌握了morse的字符指令,就可以使用一个简单的列表理解和dict.get()功能来翻译成morse。注意,这会将任何非字母数字字符转换为空格。正如您在另一个答案中看到的,您可以很容易地将这些字符添加到dict中。这将确保所有字符都匹配,因为Morse不区分大小写。在

def txt_2_morse(msg):

    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':' ..', '1':'.  ', '2':'.. -', '3':'... ', '4':'....-',
        '5':'.....', '6':'-....', '7':' ...', '8':' -..', '9':'  .',
        '0':'  -'}

    return "".join([morse.get(c.upper(), ' ') for c in msg])

print(txt_2_morse("Hello World"))
# ......-...-.. - .  -.-..-..-..

因此,如果您想逐行将一个文件读入一个输出文件,您可以简单地分别解析每一行。在

^{pr2}$

输入:

this is a file
with a few lines in it
to demonstrate code

输出:

-......... ..... .- ..-....-... 
. ..-.... .- ..-...  .-....-..... ..-. ..- 
   -...   ....-.-.. . -.-. -.
MORSE_CODE_DICT = { '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 encrypt(message):
    cipher = ''
    message_upper=message.upper()
    for letter in message_upper:
        if letter != ' ':
            if letter in MORSE_CODE_DICT:
                cipher += MORSE_CODE_DICT[letter] + ' '
            else:
                cipher+=letter
        else:
             cipher += ' '
    return cipher

O/P:-
>>> encrypt('I like apples + bananas!')
'..  .-.. .. -.- .  .- . . . . .-.. . ...  + -... .- -. .- -. .- ... !'

相关问题 更多 >