Java Properties in Python

Get code from GitHub

Read the key, element pairs from a java properties file

while natural_line:
    # skip blank lines and comment lines, process only valid logical lines
    if natural_line.strip() and COMMENT_LINE.match(natural_line) is None:
        logical_line = natural_line.lstrip().rstrip(LINE_BREAKS)
        # remove multi line delim and append adjacent lines
        while MULTI_LINE.match(logical_line): 
            logical_line = logical_line.rstrip()[:-1] + propfile.readline().lstrip().rstrip(LINE_BREAKS)
        pair = SPLIT_DELIM.split(logical_line, 1)
        if len(pair) == 1: pair.append(DEFAULT_ELEMENT)
        pair = [re.sub(VALID_ESC_DELIM, '', item) for item in pair]
        pair = [re.sub(ESCAPED_ESC_DELIM, ESC_DELIM, item) for item in pair]
        pair = [unicode(item, 'unicode_escape') for item in pair] 
        result[pair[0]] = pair[1] # add key, element to result dict
    natural_line = propfile.readline()
return result

Follows the java properties file format and tested against the Java 6 version of java.util.Properties

Get code from GitHub