繁体   English   中英

在Python中,如何读取输入文件,然后将其转换为莫尔斯电码?

[英]In Python, how do I read an input file to then translate it to morse code?

我必须创建一个向用户询问输入文件的程序,然后创建一个包含以摩尔斯电码编码的消息的输出文件。 当我运行程序时,“翻译的+ =字母[单词]”行中有类型错误,它说这是不可散列的类型:“列表”。 打开文件后,如何将输入文件中的文本转换为莫尔斯电码?

函数之后的代码有问题吗?

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)
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!')
'..  .-.. .. -.- .  .- .--. .--. .-.. . ...  + -... .- -. .- -. .- ... !'

掌握了莫尔斯语字符后,您可以使用简单的列表理解和dict.get()功能将其翻译为莫尔斯语。 请注意,这会将所有非字母数字字符转换为空格。 正如您在其他答案中看到的那样,您可以轻松地将这些字符添加到字典中。 您需要注意的一个关键是str.upper()方法。 由于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"))
# ......-...-..--- .-----.-..-..-..

因此,如果要逐行将文件读取到输出文件中,则可以简单地单独解析每一行。

with open('inputfile.txt') as infile:
    with open('outputfile.txt', 'w') as outfile:
        for line in infile:
            outfile.write("{}\n".format(txt_2_morse(line)))

输入:

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

输出:

-......... ..... .- ..-....-... 
.--..-.... .- ..-...-- .-....-..... ..-. ..- 
---- -...------....-.-..--. -.-.---.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM