Properties file in python (similar to Java Properties)

后端 未结 25 2815
故里飘歌
故里飘歌 2020-11-29 17:41

Given the following format (.properties or .ini):

propertyName1=propertyValue1
propertyName2=propertyValue2
...
propertyNam         


        
25条回答
  •  Happy的楠姐
    2020-11-29 18:20

    This is a one-to-one replacement of java.util.Propeties

    From the doc:

      def __parse(self, lines):
            """ Parse a list of lines and create
            an internal property dictionary """
    
            # Every line in the file must consist of either a comment
            # or a key-value pair. A key-value pair is a line consisting
            # of a key which is a combination of non-white space characters
            # The separator character between key-value pairs is a '=',
            # ':' or a whitespace character not including the newline.
            # If the '=' or ':' characters are found, in the line, even
            # keys containing whitespace chars are allowed.
    
            # A line with only a key according to the rules above is also
            # fine. In such case, the value is considered as the empty string.
            # In order to include characters '=' or ':' in a key or value,
            # they have to be properly escaped using the backslash character.
    
            # Some examples of valid key-value pairs:
            #
            # key     value
            # key=value
            # key:value
            # key     value1,value2,value3
            # key     value1,value2,value3 \
            #         value4, value5
            # key
            # This key= this value
            # key = value1 value2 value3
    
            # Any line that starts with a '#' is considerered a comment
            # and skipped. Also any trailing or preceding whitespaces
            # are removed from the key/value.
    
            # This is a line parser. It parses the
            # contents like by line.
    

提交回复
热议问题