Python 关于从密钥文件制作解密程序的一点帮助?

Python 关于从密钥文件制作解密程序的一点帮助?,python,encryption,Python,Encryption,好吧,我已经挣扎了好几天试图弄明白这一点。我被指派制作一个标记为“cipher.py”的代码,该代码导入一个加密的文本文件,使用“key.txt”进行解密,然后将解密的信息写入“decrypted.txt”之类的内容 我一直在获取encryptedWords=encryptedWords.replace(str(encryptedFiles[I][1])、str(decryptedFiles[I][0])) AttributeError:“列表”对象没有属性“替换” 有什么想法吗 key = o

好吧,我已经挣扎了好几天试图弄明白这一点。我被指派制作一个标记为“cipher.py”的代码,该代码导入一个加密的文本文件,使用“key.txt”进行解密,然后将解密的信息写入“decrypted.txt”之类的内容

我一直在获取encryptedWords=encryptedWords.replace(str(encryptedFiles[I][1])、str(decryptedFiles[I][0])) AttributeError:“列表”对象没有属性“替换”

有什么想法吗

key = open("key.txt", "r")
encrypted = open("encrypted.txt","r" )
decrypted = open("decrypted.txt", "w")

encryptedFiles = []
decryptedFiles = []
unencrypt= ""


for line in key:
 linesForKey = line.split()
 currentEncrypted = (line[0])
 currentDecrypted = (line[1])


encryptedFiles.append(currentEncrypted)
decryptedFiles.append(currentDecrypted)

key.close()


##########################################


encryptedWords = []
encryptedString = ""
lines = encrypted.readlines()
for line in lines:
 letter = list(line)
 encryptedWords += letter

for i in range(len(encryptedWords)):
 encryptedWords = encryptedWords.replace(str(encryptedFiles[i][1]),str(decryptedFiles[i][0]))

encrypted.close()
decrypted.write(encryptedWords)
decrypted.close()
。正如我看到的那样,您正在
+=
ing,您只需将
加密字
添加为字符串即可

+=
ing有趣地与列表一起工作:

>>> array = []
>>> string = ''
>>> string+='hello'
>>> array+='hello'
>>> string
'hello'
>>> array
['h', 'e', 'l', 'l', 'o']
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
>>> 
+=
接受字符串,拆分必须像
列表(字符串)
那样进行。与此相反,您可以将
encryptedWords
更改为字符串(
encryptedWords='
),也可以使用
'.join(encryptedWords)
'.join(encryptedWords)
转换回您想要的字符串:

>>> array = list('hello')
>>> array
['h', 'e', 'l', 'l', 'o']
>>> ''.join(array)
'hello'
>>> ' '.join(array)
'h e l l o'
>>>