Skip to content

Instantly share code, notes, and snippets.

@arunm8489
Created June 3, 2020 13:46
Show Gist options
  • Save arunm8489/f0f6e0c7cecd46adbb66269ea1eb66e6 to your computer and use it in GitHub Desktop.
Save arunm8489/f0f6e0c7cecd46adbb66269ea1eb66e6 to your computer and use it in GitHub Desktop.
def parse_cfg(config_file):
file = open(config_file,'r')
file = file.read().split('\n')
file = [line for line in file if len(line)>0 and line[0] != '#']
file = [line.lstrip().rstrip() for line in file]
final_list = []
element_dict = {}
for line in file:
if line[0] == '[':
if len(element_dict) != 0: # appending the dict stored on previous iteration
final_list.append(element_dict)
element_dict = {} # again emtying dict
element_dict['type'] = ''.join([i for i in line if i != '[' and i != ']'])
else:
val = line.split('=')
element_dict[val[0].rstrip()] = val[1].lstrip() #removing spaces on left and right side
final_list.append(element_dict) # appending the values stored for last set
return final_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment