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

2024-04-19 07:21:42 发布

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

好吧,我已经挣扎了好几天了。我被指派去做一个代码,上面写着密码.py导入加密文本文件的密钥.txt要解密,请将解密的信息写入解密.txt““

我一直收到加密字=加密字.replace(str(加密文件[i][1]),str(解密文件[i][0])) AttributeError:“list”对象没有“replace”属性

你知道吗?你知道吗

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()

Tags: 文件keyintxtforcloselineopen
1条回答
网友
1楼 · 发布于 2024-04-19 07:21:42

The ^{} attribute is only for strings。如我所见,您可以将+=添加为字符串。你知道吗

+=有趣的是,ing使用列表:

>>> array = []
>>> string = ''
>>> string+='hello'
>>> array+='hello'
>>> string
'hello'
>>> array
['h', 'e', 'l', 'l', 'o']
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
>>> 

+=接受字符串,拆分必须与list(string)一样。与此相反,您可以将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'
>>> 

相关问题 更多 >