Writing columns from a text file to a list of dicts in python
我有一个包含四列的文本文件:时间序列域服务器
文本文件的内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 | 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列表的最佳方法是什么:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [{"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}]} ] }] |
号
行的顺序可以更改,但列始终保持不变。
也许不是最好的方法,但在某些方面是有益的:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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()]) |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | {'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次才能这样做,但是这仍然比为插入的每一行遍历列表要好。