简体   繁体   English

将项目添加到列表列表 - Python

[英]Adding items to list of lists - Python

I am looking to use the code I have here to match domains to their DNS resolver name我希望使用我在这里的代码将域与其 DNS 解析器名称匹配

Current CSV output当前 CSV 输出

domain1 dns1 dns2 dns3 \n domain2 dns1 dns2 dns3 \n etc

This is the incorrect format, because it is adding all domains and dns resolvers to the same row, instead of a new row based on the new domain.这是不正确的格式,因为它将所有域和 dns 解析器添加到同一行,而不是基于新域的新行。 They are only separated by a blank cell because of the newline character.由于换行符,它们仅由一个空白单元格分隔。 I instead want it to be written as below, where each domain & its dns resolvers are written to their own individual row.相反,我希望它写成如下,其中每个域及其 dns 解析器都被写入自己的单独行。

Expected CSV output:预期的 CSV 输出:

domain1 dns1 dns2 dns3
domain2 dns1 dns2 dns3
domain3 dns1 dns2 dns3
etc...

I want the CSV file to be written out in the correct format, and with the code that I have, every time a domain is passed to def dns_resolver, it should iterate to a new list index.我希望以正确的格式写出 CSV 文件,并且使用我拥有的代码,每次将域传递给 def dns_resolver 时,它都应该迭代到新的列表索引。 that way, each domain, and it's dns resolvers have their own list, so when writing out to a new CSV file, each domain will be printed in it's own row in the CSV file.这样,每个域及其 dns 解析器都有自己的列表,因此在写入新的 CSV 文件时,每个域都将打印在 CSV 文件中自己的行中。

The code is not iterating through the list index correctly, and does not add the domain & its dns names to any list because of this.代码没有正确遍历列表索引,因此不会将域及其 dns 名称添加到任何列表中。 When they are written all into the same list, it works fine, but they are written out all into the same row, which is incorrect.当它们全部写入同一个列表时,它工作正常,但它们全部写入同一行,这是不正确的。 So instead of using 1 list, I am going to use a list of lists, and write each to its own list, and then write each list to the csv file, so that they are in their own rows.因此,我将使用一个列表列表,而不是使用 1 个列表,并将每个列表写入自己的列表,然后将每个列表写入 csv 文件,以便它们在自己的行中。 Normally the domains will be read into a list from a csv file, but for the sake of this, I entered 3 values.通常域会从 csv 文件读入列表,但为此,我输入了 3 个值。

import dns.resolver 
import csv
import os
from os.path import dirname, abspath
r = 0

def dns_resolver(domain):
    server = []
    resolvers = []
    
    try:
        resolvers = dns.resolver.resolve(domain, 'NS')
        #dns_list.append(domain)
        for x in resolvers:
            #dns_list.append(x.target)
            #dns_list.append('\n') 
            server.append(str(x.target))
    except:
        server.append('did not resolve')
    finally:
        return (domain, *server)
            

# Read in all domains from csv file domains.csv & count how many domains there are listed
domain_list = ['google.com', 'facebook.com', 'github.com']
domain_amount = 0

with open(domainFName, 'r') as file:
    for line in csv.reader(file):
        name = (line)
        domain_list.append(line)
        domain_amount += 1

for first_domain in domain_list:
    for x in first_domain:
        outputWriter.writerow(dns_resolver(x))
  • You can simply make your dns_resolver function return a list for given domain.您可以简单地让您的dns_resolver函数返回给定域的列表。
  • The *server is a shorthand to append each item into a list. *server是将每个项目附加到列表中的简写。
  • Using list comprehension, collect all lists into a list of lists to write to CSV.使用列表理解,将所有列表收集到要写入 CSV 的列表列表中
def dns_resolver(domain):
    # do your dns resolution
    # server = dns.resolver.resolve(domain, 'NS')
    server = ["dns1", "dns2", "dns3", "dns4"]
    return [domain, *server]
        

# Read in all domains
domain_list = ['google.com', 'facebook.com', 'github.com']

print([dns_resolver(d) for d in domain_list])

Output:输出:

[
   ['google.com', 'dns1', 'dns2', 'dns3', 'dns4'],
   ['facebook.com', 'dns1', 'dns2', 'dns3', 'dns4'],
   ['github.com', 'dns1', 'dns2', 'dns3', 'dns4']
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM