繁体   English   中英

将文件读入python字典

[英]reading a file into a python dictionary

我有一个这样的文本文件

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

我想读入字典。这是代码。

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)

以下是print语句的输出。

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表示,但是print语句仅打印其中之一。 并非所有键值对都保存在词典中。 并且我也希望输入中只有一个IP地址的行被忽略,这是在此代码中完成的。谢谢。

字典不能那样工作。 当您将值分配给已经具有值的键时,先前的值将被覆盖。

也许尝试使每个字典值成为一个列表,然后可以将其附加到:

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

Python字典是有条件的:键必须唯一,不能有多个equals键。 如果您尝试分配一个已经存在的键,它将被覆盖(实际上,您将具有该键的最后一行作为值)。
参见http://docs.python.org/2/tutorial/datastructures.html#dictionaries

您既可以将列表用作值,也可以附加新值,也可以使用MultiDicts ,即允许多个相等键的特殊字典。

使用toolz库的功能解决方案

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM