Kevin R.
Kevin R.

Reputation: 602

Adding lines of a text file to a dictionary

I've been trying to think how I would exactly do this, but I cant seem to get anywhere.

If I have a text file that contains a host name with their made up corresponding ip address:

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

How could I get the address and their corresponding ip address in a list or dictionary?

So like for www.google.com would be something like:

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

The lines above will always be in the same format, so I could iterate over the file, take the above, use split(), and add the addresses to a dictionary.

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

The key would be just the website, and the values would be all of its corresonpding ip addresses. I just need to get them inside a list or something togethe.

Upvotes: 1

Views: 93

Answers (4)

Ohad Eytan
Ohad Eytan

Reputation: 8464

Use dictionary of lists. For simple implementation use defaultdict as follows:

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

Upvotes: 2

Sunny Patel
Sunny Patel

Reputation: 8077

Like others have stated, it's easy to use defaultdict to prime the values of your domain keys as a list, and just append the IP addresses to that list.

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']})

You can shorten the number of lines and still make sense by pushing each line into str.split. If your file is massive, you can switch to using imap from itertools instead (same syntax) to conserve memory.

Upvotes: 1

Abhishek Balaji R
Abhishek Balaji R

Reputation: 675

Using a dictionary, you can do this:

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] 

Upvotes: 0

R Nar
R Nar

Reputation: 5515

You can make use of defaultdict from collections and set your default as a list:

>>> 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']

Upvotes: 3

Related Questions