我有一个包含四列的文本文件: 时间 序列 域 服务器
文本文件的内容如下:
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
阅读此文件并创建dict列表的最佳方法是:
[{"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}]}
]
}]
行的顺序可能会改变,但列始终保持不变。
答案 0 :(得分: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'}]
好处是,很容易键入字典,只循环一次以创建插入。
唯一的缺点是它不是相同的格式,并且必须再循环一次这样做,但是这仍然需要迭代列表中的每一行被插入。