我目前使用此代碼的頂部部分是正確的,但我不能似乎將它保存到文件遇到問題將數據寫入到文件
這是我當前的代碼
for line in myfile:
list_of_line = line.split()
if 'Failed password for' in line:
ip_address_port = list_of_line[-4]
ip_address_list = ip_address_port.split(':')
ip_address = ip_address_list[0]
print '\'',ip_address,'\''
if ips_desc.has_key(ip_address):
count_ip = ips_desc[ip_address]
count_ip + count_ip +1
ips_desc[ip_address] +=1
count_ip =0
else:
ips_desc[ip_address] = 1
print ips_desc
myfile = open('blacklist.txt','w')
for ips_items in ips_desc.keys():
myfile.write(ips_items)
但最後3行不工作任何想法?
你會得到什麼錯誤信息? – 2012-03-08 17:25:22
另外,它可能是堆棧溢出格式,但最後一行應該有一個縮進。 – 2012-03-08 17:26:04
我認爲myfile.write是縮進了一個,否則這將是問題 – 2012-03-08 17:26:43