python在列表

时间:2015-10-29 20:08:56

标签: python

有谁可以告诉我为什么底部的for / if语句没有在列表domaininfo中将空字符串设置为None

while True:
    line = proc.stdout.readline()
    domaininfo = line.split(',')

    if line!= '':
        Id = None
        fakedomain = domaininfo[1]
        A = domaininfo[2]
        AAAA = domaininfo[3]
        MX = domaininfo[4]
        NS = domaininfo[5]
        country = domaininfo[6]
        whois_created = domaininfo[7]
        whois_updated = domaininfo[8]
        SSDEEP = domaininfo[9]

        for item in domaininfo[1:]:
            if not item:
                item = None

输出是:

['blah.com', '1.1.1.1', '', 'mx1.somemxserver.com', 'ns2.somensserver.net', 'Spain', '', '', '\n']

期望的输出:

['blah.com', '1.1.1.1', None, 'mx1.somemxserver.com', 'ns2.somensserver.net', 'Spain', None, None, '\n']

1 个答案:

答案 0 :(得分:2)

您正在设置变量,而不是列表条目:

for i, item in enumerate(domaininfo[1:]):
    if not item:
        domaininfo[i + 1] = None