0

I have a text file like this

128.220.251.50
130.79.48.57
203.110.240.191
128.220.251.50 208.94.63.193
128.36.233.154 
128.36.233.154 131.246.112.29
128.36.233.154 136.145.115.196
130.79.48.57 203.110.240.191
131.246.112.29 199.26.254.68
136.145.115.196 128.220.251.50
136.145.115.196 140.247.60.123
137.165.1.113 
137.165.1.113 128.220.251.50
137.165.1.113 128.36.233.154
137.165.1.113 130.79.48.57
140.247.60.123 137.165.1.113
199.26.254.68 136.145.115.196
203.110.240.191 131.246.112.29
208.94.63.193 140.247.60.123

I want to read this into a dictionary.This is the code.

def get_key_value(line):
  key, sep, value = line.strip().partition(" ")

  return key, value

with open("output.txt") as fd:    
    d = dict(get_key_value(line) for line in fd)

for key,value in d.iteritems():
    print str(key),str(value)

The following is the output for the print statement.

128.220.251.50 208.94.63.193
130.79.48.57 203.110.240.191
203.110.240.191 131.246.112.29
131.246.112.29 199.26.254.68
199.26.254.68 136.145.115.196
136.145.115.196 140.247.60.123
128.36.233.154 136.145.115.196
140.247.60.123 137.165.1.113
208.94.63.193 140.247.60.123
137.165.1.113 130.79.48.57

I have the following problem.If u consider the input there are three keys(or lines) stating with 137.165.1.113.But the print statement is printing just one of the them. not all key value pairs are saved in the dictionary. and also i want the lines with just one ip address in the input to be ignored which is done in this code.Thanks in advance.

3 Answers 3

2

Dictionaries don't work like that. When you assign a value to a key that already has a value, the previous value gets overridden.

Perhaps try making each dictionary value a list which you can then append to:

d = {}
with open("output.txt") as fd:
    for line in fd:
        if not line.count(' '): continue # Skip over non-splittable lines
        for k,v in line.split():
            if k in d:
                d[k].append(v)
            else:
                d[k] = [v] 

for key,value in d.iteritems():
    print str(key), " ".join(value))
Sign up to request clarification or add additional context in comments.

4 Comments

Nice answer Chris. Was about to post mine when i see this :D
Also, you forgot to declare line.
for k,v in get_key_value(line): ValueError: need more than 0 values to unpack I defined line
@aIKid Oops, added line
1

Python dictionaries are sets: keys must be unique, you can't have multiple equals key. If you try to assign an already esisting key, it gets overridden (infact you have as value the last line with that key).
See http://docs.python.org/2/tutorial/datastructures.html#dictionaries

You can either use lists as values, and append the new value or use MultiDicts, special dictionaries that allow multiple equal keys.

Comments

0

A functional solution using the toolz library

$ pip install toolz
$ python

>>> from toolz import groupby, valmap, first, second

>>> with open(filename) as f:
...     lines = [line.strip().split(' ') for line in f if ' ' in line]

>>> groupby(first, lines)
{'128.220.251.50': [['128.220.251.50', '208.94.63.193']],
 '128.36.233.154': [['128.36.233.154', '131.246.112.29'], ['128.36.233.154', '136.145.115.196']],
 '130.79.48.57': [['130.79.48.57', '203.110.240.191']],
 '131.246.112.29': [['131.246.112.29', '199.26.254.68']],
 '136.145.115.196': [['136.145.115.196', '128.220.251.50'], ['136.145.115.196', '140.247.60.123']],
 '137.165.1.113': [['137.165.1.113', '128.220.251.50'], ['137.165.1.113', '128.36.233.154'], ['137.165.1.113', '130.79.48.57']],
 '140.247.60.123': [['140.247.60.123', '137.165.1.113']],
 '199.26.254.68': [['199.26.254.68', '136.145.115.196']],
 '203.110.240.191': [['203.110.240.191', '131.246.112.29']],
 '208.94.63.193': [['208.94.63.193', '140.247.60.123']]}

>>> valmap(lambda L: map(second, L), _)
{'128.220.251.50': ['208.94.63.193'],
 '128.36.233.154': ['131.246.112.29', '136.145.115.196'],
 '130.79.48.57': ['203.110.240.191'],
 '131.246.112.29': ['199.26.254.68'],
 '136.145.115.196': ['128.220.251.50', '140.247.60.123'],
 '137.165.1.113': ['128.220.251.50', '128.36.233.154', '130.79.48.57'],
 '140.247.60.123': ['137.165.1.113'],
 '199.26.254.68': ['136.145.115.196'],
 '203.110.240.191': ['131.246.112.29'],
 '208.94.63.193': ['140.247.60.123']}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.