在第一次比较循环后调用列表中的下一次迭代

时间:2016-09-19 04:26:29

标签: python python-3.x

我有一种情况,我必须循环一个列表,从文件中获取信息,写一个新文件,发送一封电子邮件,然后返回列表进行下一次迭代,重新开始这个过程。登记/> 我会尝试写出来,然后把我的代码放进去看看。
列表是ipaddresses的列表,两个文件都是文本。

从文件中获取信息1 def构建我的list1(第一个代码块)
def take list1 [0]将它应用于file1并从中构建file2(第二个代码块)
def从list1 [0]获取电子邮件地址 def发送带有file2的电子邮件 (我可以做到这一切,它只是回到列表1 [1]我无法做到)
转到list1 [1]申请file1 build file2发送电子邮件等......直到列表末尾

  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是iter)而不是循环)

  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
我读书的时候喜欢学习,但我被困了。指出我正确的方向,我会阅读并回来回答这个问题 然而,根据我所读到的,人们希望构建过滤器,更多功能。

我认为我应该很容易掌握它 (帖子有点长,但想要通过)

1 个答案:

答案 0 :(得分: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,line是str。 字符串永远不会包含list_iter。

你可以试试这个:

if line in one_ip_each.ipAddr
相关问题