I need to grab strings from all files in a directory and record them somehow, so I tried to use defaultdict to create this, but I'm having difficulty figuring out how to progressively add to each layer of the dictionary. Essentially, what the dictionary should look like is this:
Filename
Bundle
Info
Bundle
Info
Filename
Bundle
Info
etc I have the info as a list so I can just append anything I need onto the list, but when I run what I have here, I end up getting a single bundle and info for each file name. It seems like the update() function is replacing the values inside and I'm not sure how to make it keep adding and then creating a newer dictionary for each Bundle. Any help is appreciated, and sorry for any confusion.
import collections
import os
devices = collections.defaultdict(lambda: collections.defaultdict(dict))
# bundles = collections.defaultdict(dict)
for filename in os.listdir('.'):
if os.path.isfile(filename):
if '.net' in filename:
dev = open(filename)
for line in dev:
line = line.strip()
values = line.split(' ')
if values[0] == 'interface':
bundle_name = values[1]
if '/' in bundle_name:
pass
else:
if len(values) == 2:
ur_device = devices[filename]
ur_device.update(
{
'bundle_name': bundle_name,
'bundle_info': [],
}
)
if 'secondary' in values:
pass
elif values[0] == 'ipv4' and values[1] == 'address' and len(values) == 4:
ur_device['bundle_info'].append(
{
'ip_address': values[2],
'subnet_mask': values[3],
}
)
dev.close()