Amistad
Amistad

Reputation: 7410

Writing columns from a text file to a list of dicts in python

I have a text file with four columns: time serial domain server

the contents of the text file are as follows:

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

What would be the best way to read this file and create a list of dict as follows:

[{"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}]}
           ]
}]

The order of the rows could change but the columns always remain the same.

Upvotes: 0

Views: 57

Answers (1)

jmunsch
jmunsch

Reputation: 24099

Perhaps not the best way, but a way that is beneficial in some ways:

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

Output:

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

The benefits being, easily keying into the dictionaries, only looping over once to create the insertions.

The only downside to this is that it is not in the same format, and has to be looped over 1 more time to do so, this however still beats having to iterate over the list for every line being inserted.

Upvotes: 1

Related Questions