Last active
August 29, 2015 14:10
-
-
Save abraithwaite/6dfe94305514d6af96ef to your computer and use it in GitHub Desktop.
Parse arbitrary key:value file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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