2015-10-27 86 views
-1
#!/C:\Users\Yue\Desktop\ 

import re 
from pprint import pprint 
from collections import defaultdict 

output = open("udp_data","w") 
sum = defaultdict(list) 

with open("DNS","r") as input: 
    ip = [] 
    port = [] 

    for line in input: 

      if re.match("(.*)Internet(.*)",line): 
        ip.append(line.split("Src: ",1)[1]) 

      if re.match("(.*)User(.*)",line): 
        port.append(line) 

count = 0       
for y in ip: 
    if y in sum: 
     sum[y].append(port[count]) 
    else: 
     sum[y]= port[count] 

    count += 1 

pprint (sum) 

誰能幫我這個錯誤,AttributeError的:「海峽」對象沒有屬性「追加」?我不知道爲什麼它一直告訴我,我不能將str附加到我爲字典定義的列表中。AttributeError的:「海峽」對象沒有屬性「追加」蟒蛇

回答

2

您收到的錯誤消息並未說明您無法將str附加到列表中。這是說你的字符串沒有append方法..

在你的代碼port是一個字符串列表。 在線:

sum[y] = port[count] 

...你要分配的元素port[count],這是str類型來sum[y]。之後,你試圖用...

sum[y].append(port[count]) 

...一個項目追加到您指定的字符串之前 - 在這一點上和[Y]`是一個字符串,而不是一個列表。

+0

非常感謝! –

相關問題