【发布时间】:2022-11-02 05:20:25
【问题描述】:
我正在尝试创建一个执行此操作的程序:
您的程序应该读取输入文件,使用提供的字典对文本进行编码,然后 将结果写入文本文件。使用函数加密文件。该函数应接受 文件的内容(作为字符串)并返回加密的内容。
到目前为止,我有这个:
code= {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8', 'D': '&', 'd': '7',
'E': '^', 'e': '6', 'F': '%', 'f': '5', 'G': '$', 'g': '4', 'H': '#', 'h': '3',
'I': '@', 'i': '2', 'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y',
'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v', 'P': 'U', 'p': 'u',
'Q': 'T', 'q': 't', 'R': 'S', 'r': 's', 'S': 'R', 's': 'r', 'T': 'Q', 't': 'Q',
'U': 'P', 'u': 'p', 'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm',
'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j', '@': 'I', '2': 'i',
'#': 'H', '3': 'h', '$': 'G', '4': 'g', '%': 'F', '5': 'f', '^': 'E', '6': 'e',
'&': 'D', '7': 'd', '*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a',
':': ',', ',': ':', '.': '?', '.': '?', '<': '>', '>': '<', "'": '"', '"': "'",
'+': '-', '-': '+', '=': ';', ';': '=', '[': '{', '{': '[', ']': '}', '}': ']'
}
first_file = open('farts.txt','r')
read_file = first_file.read()
first_file.close()
def encrypt(s):
encrypt_file = open('ENCRYPTED_Plain_Text_File.txt','w')
for i in s:
if i in code:
encrypt_file.write(code[i])
else:
encrypt_file.write(i)
return str(encrypt_file)
encrypt(read_file)
【问题讨论】:
-
那么,你能描述一下确切的问题吗?它会崩溃,是否有错误,或者是否有关于预期输出的错误输出?帮助我们帮助您。另见tour和How to Ask。
标签: python encryption