将文本文件的行添加到字典中

时间:2016-08-08 17:34:01

标签: python list dictionary

我一直在想我将如何做到这一点,但我似乎无法到达任何地方。

如果我的文本文件包含一个主机名及其相应的IP地址:

The result of www.espn.com is 199.181.133.15
The result of www.espn.com is 199.454.152.10
The result of www.espn.com is 20.254.215.14
The result of www.google.com is 141.254.15.14
The result of www.google.com is 172.14.54.153
The result of www.yahoo.com is 181.145.254.12

如何在列表或字典中获取地址及其对应的IP地址?

因此对于www.google.com来说就像是:

("www.google.com", 141.254.15.14, 172.14.54.153)

上面的行总是采用相同的格式,因此我可以遍历文件,使用上面的内容,使用split(),然后将地址添加到字典中。

  ....... 
        ....
            dictA = {}
            for line in f:
                splitLine = line.split()
                    dictA = {splitLine[2]: splitLine[3]}

密钥只是网站,价值就是它的所有相应的IP地址。我只需要将它们放在列表或其他东西中。

4 个答案:

答案 0 :(得分:3)

您可以使用defaultdict中的collections并将默认设置为列表:

>>> from collections import defaultdict
>>> s = '''The result of www.espn.com is 199.181.133.15
... The result of www.espn.com is 199.454.152.10
... The result of www.espn.com is 20.254.215.14
... The result of www.google.com is 141.254.15.14
... The result of www.google.com is 172.14.54.153
... The result of www.yahoo.com is 181.145.254.12'''.splitlines()
>>> dictA = defaultdict(list)
>>> for line in s:
...     words = line.split()
...     dictA[words[3]].append(words[-1])
...
>>> dictA
defaultdict(<type 'list'>, {'www.yahoo.com': ['181.145.254.12'], 'www.espn.com': ['199.181.133.15', '199.454.152.10', '20.254.215.14'], 'www.google.com': ['141.254.15.14', '172.14.54.153']})

>>> for key, val in dictA.items():
...     print key, val
...
www.yahoo.com ['181.145.254.12']
www.espn.com ['199.181.133.15', '199.454.152.10', '20.254.215.14']
www.google.com ['141.254.15.14', '172.14.54.153']

答案 1 :(得分:2)

使用列表字典。对于简单实现,请使用defaultdict,如下所示:

from collections import defaultdict
dictA = defaultdict(list)
for line in f:
    splitLine = line.split()
    dictA[splitLine[3]].append(splitLine[5])

答案 2 :(得分:1)

与其他人一样,可以很容易地使用str.split键的值作为列表填充,然后将IP地址附加到该列表中。

from collections import defaultdict

dictA = defaultdict(list)
with open('filename', 'r') as f:
    #Where domain is the 4th item in the line, and ip is the 6th
    for domain, ip in ((line[3], line[5]) for line in map(str.split, f.readlines())):
        dictA[domain].append(ip)

print dictA
  

defaultdict(,{'www.yahoo.com':['181.145.254.12'],'www.espn.com':['199.181.133.15','199.454.152.10','20 .254.215.14'] ,'www.google.com':['141.254.15.14','172.14.54.153']})

您可以通过将每一行推入imap来缩短行数并仍然有意义。如果您的文件很大,您可以切换为使用itertools中的{{3}}代替(语法相同)来节省内存。

答案 3 :(得分:0)

使用字典,您可以这样做:

domain_name_to_ip_mappping = {}
with open('filename') as f:
    for line in f:

        data = line.split()
        domain_name = data[3]
        ip = data[-1]
        if domain_name in domain_name_to_ip_mappping:
            #domain name already exists, so simply append ip
            domain_name_to_ip_mappping[domain_name].append(ip) 
        else:  
            #create a domain entry and init a list with current ip 
            domain_name_to_ip_mappping[domain_name] = [ip]