37 lines
835 B
Python
37 lines
835 B
Python
def loadMorse(filename: str, to_ascii: bool):
|
|
fio = open(filename, "r")
|
|
|
|
conv_table = {}
|
|
|
|
for line in fio:
|
|
line = line.split()
|
|
|
|
if to_ascii:
|
|
conv_table[line[1].upper()] = line[0]
|
|
else:
|
|
conv_table[line[0]] = line[1].upper()
|
|
|
|
conv_table[" "] = " "
|
|
|
|
return conv_table
|
|
|
|
|
|
def main():
|
|
to_ascii = input("Vuoi decodificare un file? ") == "si"
|
|
in_file = input("Inserisci il nome del file: ")
|
|
|
|
conv_table = loadMorse("morse.txt", to_ascii)
|
|
|
|
with open(in_file, "r") as fio:
|
|
in_str = fio.readline()
|
|
|
|
if to_ascii:
|
|
for char in in_str.split():
|
|
print(conv_table[char], end="")
|
|
else:
|
|
in_str = list(in_str.upper())
|
|
for char in in_str:
|
|
print(conv_table[char], end=" ")
|
|
|
|
|
|
main() |