循环输出保留最后的输出而不更新输出

时间:2019-02-11 18:06:05

标签: python-3.x

我有一个循环,循环中有许多变量作为输出...有些变量保留了之前通过的输出,而不是给我新的输出。即使找不到输出,它也应该返回None,而不是重复最后的输出。

for line in csv_urls:
   website = line[0]
   print(website)
   try:
      f = urllib.request.urlopen(line[0])
      a = f.read().decode('utf-8')
      cnpj_final = "Not Found"
      cnpj = None
      cnpj = re.findall(r"\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}", a)
      if cnpj != []:
           x = cnpj
           x = set(x)
           seen_cnpj = set()
           cnpj_final = []
           for item in x:
              if item not in seen_cnpj:
                  seen_cnpj.add(item)
                  cnpj_final.append(item)
              else:
                  cnpj_final = None
       else:
           cnpj_final = "Not Found"
       print("CNPJ:", cnpj_final, '\n')
   except:
         pass
         cnpj_final = None

>>> https://www.medsam.com.br
    CNPJ: ['20.104.619/0001-53']

>>> https://www.clamarrocaplus.com.br
    CNPJ: ['20.104.619/0001-53']

>>> https://www.anisboutique.com.br
    CNPJ: ['31.410.120/0001-90']

一旦找不到第二个网站的CNPJ,它就会保留第一个网站。我如何阻止这种情况发生?找到第三个CNPJ,并重新确定输出。

0 个答案:

没有答案