Created
November 8, 2018 03:48
-
-
Save dtranhuusm/f3edc64589ae71176eae0772b103a454 to your computer and use it in GitHub Desktop.
Build a dictionary from properties 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
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