2014-04-17 30 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' 
>>> 
相關問題