Skip to content

Instantly share code, notes, and snippets.

@abraithwaite
Last active August 29, 2015 14:10
Show Gist options
  • Save abraithwaite/6dfe94305514d6af96ef to your computer and use it in GitHub Desktop.
Save abraithwaite/6dfe94305514d6af96ef to your computer and use it in GitHub Desktop.
Parse arbitrary key:value file.
identifiers = ["IP Address", "Vlan", "Subnet Mask"] # etc. Define what you want to grab here
def parse_line(line):
# the second argument means only take the first split. e.g. "a:b:c" becomes "a", "b:c"
key, value = line.split(":", 1)
# if the identifier is one we care about
if key in identifiers:
return key, value
return None, None
with open("filename.txt", 'r') as f:
lines = f.readlines()
for line in lines:
k, v = parse_line(lines)
# If you want to parse multiple interfaces
# in the same file, watch out for key collisions
if k:
info[k] = v
print(info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment