【问题标题】:Writing columns from a text file to a list of dicts in python将文本文件中的列写入python中的字典列表
【发布时间】:2017-08-11 08:44:31
【问题描述】:

我有一个包含四列的文本文件: 时间 序列 服务器

文本文件内容如下:

15 14 google.com 8.8.8.8
19 45 google.com 8.8.4.4
98 76 google.com 208.67.222.222
20 23 intuit.com 8.8.8.8
45 89 intuit.com 8.8.4.4
43 21 intuit.com 208.67.222.222
78 14 google.com 8.8.8.8
92 76 google.com 8.8.4.4
64 54 google.com 208.67.222.222
91 18 intuit.com 8.8.8.8
93 74 intuit.com 8.8.4.4
65 59 intuit.com 208.67.222.222

读取此文件并创建字典列表的最佳方法如下:

[{"server":"8.8.8.8", 
  "domains":[{"google.com":[{"time":15, "serial":14}, {"time":78, "serial":14}]},
             {"intuit.com":[{"time":20, "serial":23}, {"time":91, "serial":18}]}
            ]
},
{"server":"8.8.4.4", 
 "domains":[{"google.com":[{"time":19, "serial":45}, {"time":92, "serial":76}]},
            {"intuit.com":[{"time":45, "serial":89}, {"time":93, "serial":74}]}
           ]
},
{"server":"206.67.222.222", 
 "domains":[{"google.com":[{"time":98, "serial":76}, {"time":64, "serial":54}]},
            {"intuit.com":[{"time":43, "serial":21}, {"time":65, "serial":59}]}
           ]
}]

行的顺序可以改变,但列始终保持不变。

【问题讨论】:

  • 您可以尝试使用pandas.read_csv。使用分隔符作为空格,添加列信息(即列名),然后使用pandas.DataFrame.to_dict 将DataFrame 转换为字典。可能需要进行一些分组才能得到你想要的结果。

标签: python list python-3.x file dictionary


【解决方案1】:

也许不是最好的方法,但在某些方面是有益的:

servers = {}
file_path = './test.file'
from pprint import pprint
with open(file_path,'rb') as f:
    for line in f:
        _time, serial, domain, ip = line.split()
        current_domains = servers.get(ip, {})
        times = current_domains.get(domain, [])
        times.append({"time": _time, "serial": serial})
        current_domains[domain] = times
        servers[ip] = current_domains
pprint(servers)
pprint([{"server": ip, "domains": [{domain: _time} for domain, _time in domains.items()]} for ip, domains in servers.items()])

输出:

    {'208.67.222.222': {'google.com': [{'serial': '76', 'time': '98'},
                                   {'serial': '54', 'time': '64'}],
                    'intuit.com': [{'serial': '21', 'time': '43'},
                                   {'serial': '59', 'time': '65'}]},
 '8.8.4.4': {'google.com': [{'serial': '45', 'time': '19'},
                            {'serial': '76', 'time': '92'}],
             'intuit.com': [{'serial': '89', 'time': '45'},
                            {'serial': '74', 'time': '93'}]},
 '8.8.8.8': {'google.com': [{'serial': '14', 'time': '15'},
                            {'serial': '14', 'time': '78'}],
             'intuit.com': [{'serial': '23', 'time': '20'},
                            {'serial': '18', 'time': '91'}]}}

[{'domains': [{'intuit.com': [{'serial': '21', 'time': '43'},
                              {'serial': '59', 'time': '65'}]},
              {'google.com': [{'serial': '76', 'time': '98'},
                              {'serial': '54', 'time': '64'}]}],
  'server': '208.67.222.222'},
 {'domains': [{'intuit.com': [{'serial': '23', 'time': '20'},
                              {'serial': '18', 'time': '91'}]},
              {'google.com': [{'serial': '14', 'time': '15'},
                              {'serial': '14', 'time': '78'}]}],
  'server': '8.8.8.8'},
 {'domains': [{'intuit.com': [{'serial': '89', 'time': '45'},
                              {'serial': '74', 'time': '93'}]},
              {'google.com': [{'serial': '45', 'time': '19'},
                              {'serial': '76', 'time': '92'}]}],
  'server': '8.8.4.4'}]

好处是,轻松键入字典,只需循环一次即可创建插入。

唯一的缺点是它的格式不同,并且必须再循环 1 次才能这样做,但这仍然优于必须为插入的每一行遍历列表。

【讨论】:

  • @jmunsch ..非常感谢..那行得通..我在stackoverflow.com/questions/42897984/…有同样问题的另一部分..应该就在你的小巷里..
  • @Amistad 我会把最后一行理解拉到一个完整的双循环中,然后看看使用类似的东西:stackoverflow.com/questions/12540817/… 如果你真的需要加快速度,那么它总是可以翻译成使用 numpy 和 people 提供的更快的数据结构。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-30
  • 1970-01-01
相关资源
最近更新 更多