未知位置时的地图列表值

时间:2017-08-31 21:55:07

标签: python

我有以下列表清单:

(['investmentseminar', '300', '', '', 'CNAME', '', 'domain.com.'], 7)
(['@', '300', '', '', '', '', '', '', '', 'CNAME', '', 'domain.com.'], 12)
(['@', '300', '', '', '', '', '', '', '', '', '', '', '', '', '', 'MX', '', '1', '', 'eu-smtp-inbound-1.com.'], 20)
(['@', '3600', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'TXT', '', 'MS=ms87183849'], 19)
(['@', '3600', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'TXT', '', 'MS=ms91398333'], 19)

它是来自带有BIND数据的解析文件,我试图提取记录类型和TTL,其中列表中项目的位置是固定的。

这是我到目前为止的代码:

lines = [['@', '', '', 'MX', '', '10', '', 'relay1.netnames.net.'],['@', '', '', 'MX', '', '20', '', 'relay2.netnames.net.'], ['@', '3600', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'TXT', '', 'MS=ms91398333'], ['@', '300', '', '', '', '', '', '', '', '', '', '', '', '', '', 'MX', '', '1', '', 'eu-smtp-inbound-1.com.'], ['domain.tld.', '3600', '', '', '', '', '', '', '', '', '', '', '', 'TXT', '', 'v=spf1 redirect=spf.domain.tld'],['a.ns.slf', '', '', '', '', '', '', '', '', '', 'A', '', '192.123.54.133'],['adfs', '', '', '', '', '', '', '', '', '', '', '', '', '', 'A', '', '192.123.67.20']]
record_set_list = []

def record_set(record):
    resource = {
        'Name': record[0],
        'TTL': record[1],
        'Type': record[4],
        'Value': record[-1]
    }
    record_set_list.append({'RecordSets': resource})

types = ['A', 'AAAA', 'CAA', 'CNAME', 'MX', 'NAPTR', 'PTR', 'SPF', 'SRV', 'TXT', 'ZONE']

for record in csv.reader(lines, delimiter=" "):
    any_in = any(i in record for i in types)
    if any_in is True:
        record_set(record)

我如何匹配TTL,Type和MX记录偏好?

任何建议都非常感谢

1 个答案:

答案 0 :(得分:0)

使用内置函数filter删除空字符串,zip使用相应的键删除剩余值,然后创建dict

def record_set(record):
    keys = ['Name', 'TTL', 'Type', 'Value']
    values = filter(None, record)
    resource = dict(zip(keys, values))
    record_set_list.append({'RecordSets': resource})