How to parse this special config file in python?
Question: I have some config files and the data format can't be changed.

One of them looks like this:

    
    
    root {
      configuration {
        field_a: "aaaa"
        field_b: "bbbb"
      }
      child {
        configuration {
            field_a: "aaa1"
            field_b: "bbb1"
        }
      }
      child {
        configuration {
            field_a: "aaa2"
            field_b: "bbb2"
        }
      }
    }
    

What i need is to parse the file and save it as JSON objects:

    
    
    {root: 
        {field_a:"aaaa",field_b:"bbbb"},
        children: [{field_a:"aaa", field_b:"bbb"}, ... ]
    }
    

Is there any way to make it possible ?


Answer: Your data format is unofficial-javascript-array. For great python, it's easy
to parse them to python dict in 4 line of codes. Then use simplejson to parse
to any json-format you like.

    
    
    s = '''                                                                                                          
    root {                                                                                                           
      configuration {                                                                                                
        field_a: "aaaa"                                                                                              
        field_b: "bbbb"                                                                                              
      }                                                                                                              
      child {                                                                                                        
        configuration {                                                                                              
            field_a: "aaa"                                                                                           
            field_b: "bbb"                                                                                           
        }                                                                                                            
      }                                                                                                              
    }                                                                                                                
    '''                                                                                                              
    
    s = s.replace("{", ":{").replace("}", "},")                                                                      
    s = "{%s}"%s                                                                                                     
    
    import re                                                                                                        
    s = re.sub(r'(\w+)"', r'\1",', s)                                                                                
    s = re.sub(r"(\w+)\s*\:", r'"\1":', s)                                                                           
    print "string:", s                                                                                               
    
    d = eval(s)                                                                                                      
    print "python dict:", d                                                                                          
    
    import simplejson as json                                                                                        
    print "json:", json.dumps(d) 
    

