Skip to content

Instantly share code, notes, and snippets.

@dtranhuusm
Created November 8, 2018 03:48
Show Gist options
  • Save dtranhuusm/f3edc64589ae71176eae0772b103a454 to your computer and use it in GitHub Desktop.
Save dtranhuusm/f3edc64589ae71176eae0772b103a454 to your computer and use it in GitHub Desktop.
Build a dictionary from properties file
prop_match = re.compile(r'\s*(?P<prop_id>\S+)\s*=\s*(?P<prop_val>\S+)') # pylint: disable=W1401
def extract_properties(filename):
""" Extract properties from file
Args:
filename(str): file path
Returns:
dict: dictionary for properties found
"""
with open(os.path.expanduser(filename)) as fp:
content = fp.read()
res = dict()
current = 0
prop = prop_match.search(content, current)
while prop:
res[prop.group('prop_id')] = prop.group('prop_val')
current = prop.end()
prop = prop_match.search(content, current)
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment