2016-09-19 101 views
0

我有一種情況,我必須遍歷列表,從文件中獲取信息,寫入新文件,發送電子郵件,然後返回到下一個列表迭代重新開始過程。
我會嘗試寫出來,然後把我的代碼在你看看。
該列表是ipaddresses的列表,並且這兩個文件都是文本。在第一次比較循環後調用列表中的下一次迭代

DEF從文件1
抓住信息DEF建立我的列表1(第一個代碼塊)
DEF採取list1的[0]其應用到file1和建造從中文件2(第二個代碼塊)
高清從中獲取電子郵件地址list1的[0]
高清發送電子郵件與文件2
(我能做到這一切,它只是又回到了列表1 [1],我不能這樣做)
去LIST1 [1]適用於file1中建立文件2發送電子郵件等...直至列表結束

2 import re 
    3 
    4 def one_ip_each(): 
    5  one_ip_each = [] 
    6  global ipAddr 
    7  with open('Invalid_names_file') as Invalid_names_file: 
    8   a = [re.search(r'((\d+\.)+\d+)', line).group() for line in \ 
    9     Invalid_names_file] 
10   for x in a: 
11    if x not in one_ip_each: 
12     one_ip_each.append(x) 
13     ipAddr = x 
14    return ipAddr 
15 ## makes an iterator that interpreter can step through, yet other funciton 
16 ## complains that it's not a string 
17 #  ipAddr = iter(one_ip_each) 
18 #  return ipAddr 
19 
20 one_ip_each() 

這裏的代碼,返回我想要的東西(不IPADDR作爲一個國際熱核實驗堆),而不是循環)

5 def isp_email_names(): 
    6  with open('Invalid_names_file') as Invalid_names_file: 
    7   with open('ISP_names_file', 'w') as ISP_names_file: 
    8    for line in Invalid_names_file: 
    9     if one_ip_each.ipAddr in line: 
10      ISP_names_file.write(str(line)) 
11      ISP_names_file.flush() 
了行號,這將幫助一些

希望。

我使ipAddr成爲一個全球性的,所以我可以從電子郵件功能調用它來知道file2會去適當的人。
我不知道這件事是否重要。

,如果我讓IPADDR到ITER(如線17和18),我得到 - >

TypeError: 'in <string>' requires string as left operand, not list_iterator 

我喜歡學習,而我看,但我很堅持。指出我在正確的方向,我會閱讀並回來回答這個問題。
還有我讀過的人想要構建過濾器,def更多的功能。

我會認爲它應該很容易,我只是無法把握它。
(職位是那種長,但想成爲thourough)

+2

格式的代碼和文本,或者沒有人願意提供幫助。 –

回答

0
0 def isp_email_names(): 
    1  with open('Invalid_names_file') as Invalid_names_file: 
    2   with open('ISP_names_file', 'w') as ISP_names_file: 
    3    for line in Invalid_names_file: 
    4     if one_ip_each.ipAddr in line: 

4號線,one_ip_each.ipAddr是list_iter,行是海峽。 字符串從不包含list_iter。

你可以試試這個:

if line in one_ip_each.ipAddr 
+0

感謝特洛伊ipAddr是行的子字符串,所以這將無法正常工作。看着做所有(ipAddr在文件中行),但沒有得到所需的輸出。 – emetib

+0

我現在把這個擱置,因爲它似乎我的測試機器有問題,我不得不停下來。腳本在其他機器上工作,但不是測試的。謝謝你們。 – emetib

相關問題