2013-10-28 89 views
0

我有這樣讀取文件到Python字典

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 

我想讀入dictionary.This這個文本文件中的代碼。

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) 

以下是打印語句的輸出。

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 

我有以下問題。如果ü考慮輸入有三個按鍵(或線)與137.165.1.113.But打印語句只是打印其中的一個說明。 並非所有鍵值對都保存在字典中。 而且我想輸入中只有一個IP地址的行被忽略,這是在此代碼中完成的。提前感謝。

回答

2

詞典不能這樣工作。當您爲已有值的鍵分配值時,先前的值會被覆蓋。

也許嘗試使每一個字典值的列表,然後您可以附加到:

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)) 
+0

尼斯答案克里斯功能的解決方案。即將發佈我的時候,我看到這個:D – aIKid

+1

此外,你忘了聲明'線路'。 – aIKid

+0

'爲k,v在get_key_value(行):' 'ValueError:需要超過0個值才能解包' 我定義了行 – Mark

1

Python字典是集:鍵必須是唯一的,你不能有多個等號鍵。如果你嘗試分配一個已經存在的鍵,它將被覆蓋(事實上,你有最後一行的值作爲該鍵)。
http://docs.python.org/2/tutorial/datastructures.html#dictionaries

您可以使用列表作爲值,並追加新值或使用MultiDicts,特殊的字典,允許多個相同的密鑰。

+0

謝謝......我們將查看它 – Mark

0

使用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']}