2014-04-17 33 views
0

好的,所以我一直在努力解决DAYS的问题。我已经分配了一个代码,将其标记为“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() 

回答

0

The .replace() attribute is only for strings。正如我看到你是+= ing,你可以添加encryptedWords作为一个字符串。

+=荷兰国际集团有趣的作品用列表:

>>> 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' 
>>> 
相关问题