Parsing a file that looks like JSON to a JSON
Question: I'm trying to figure out was is the best way to go about this problem: I'm
reading text lines from a certain buffer that eventually creates a certain log
that looks something like this:

    
    
    Some_Information: here there's some information about date and hour
    Additional information: log summary #1234:
    details {
      name: "John Doe"
      address: "myAdress"
      phone: 01234567
    }
    
    information {
      age: 30
      height: 1.70
      weight: 70
    }
    

I would like to get all the fields in this log to a dictionary which I can
later turn into a json file, the different sections in the log are not
important so for example if myDictionary is a dictionary variable in python I
would like to have:

    
    
    > myDictionary['age']
    

will show me `30`.

and the same for all other fields.

Speed is very important here that's why I would like to just go through every
line once and get it in a dictionary

My way about doing this would be to for each line that contains ":" colon I
would split the string and get the key and the value in the dictionary. is
there a better way to do it? Is there any python module that would be
sufficient?

If more information is needed please let me know.

Edit: So I've tried something that to me look to work best so far, I am
currently reading from a file to simulate the reading of the buffer

My code:

    
    
    import json
    import shlex
    
    newDict = dict()
    
    with open('log.txt') as f:
      for line in f:
        try:
            line = line.replace(" ", "")
            stringSplit = line.split(':')
            key = stringSplit[0]
            value = stringSplit[1]
            value = shlex.split(value)
            newDict[key] = value[0]
        except:
            continue
    with open('result.json', 'w') as fp:
       json.dump(newDict, fp)
    

Resulting in the following .json:

    
    
    {"name": "JohnDoe", "weight": "70", "Additionalinformation": "logsummary#1234",
     "height": "1.70", "phone": "01234567", "address": "myAdress", "age": "30"}
    


Answer: You haven't described exactly what the desired output should be from the
sample input, so it's not completely clear what you want done. So I guessed
and the following only extracts data values from lines following one that
contains a `'{'` until one with a `'}'` in it is encountered, while ignoring
others.

It uses the `re` module to isolate the two parts of each dictionary item
definition found on the line, and then uses the `ast` module to convert the
value portion of that into a valid Python literal (i.e. string, number, tuple,
list, dict, bool, and `None`).

    
    
    import ast
    import json
    import re
    
    pat = re.compile(r"""(?P<key>\w+)\s*:\s*(?P<value>.+)$""")
    
    data_dict = {}
    with open('log.txt', 'rU') as f:
        braces = 0
        for line in (line.strip() for line in f):
            if braces > 0:
                match = pat.search(line)
                if match and len(match.groups()) == 2:
                    key = match.group('key')
                    value = ast.literal_eval(match.group('value'))
                    data_dict[key] = value
            elif '{' in line:
                braces += 1
            elif '}' in line:
                braces -= 1
            else:
                pass  # ignore line
    
    print(json.dumps(data_dict, indent=4))
    

Output from your example input:

    
    
    {                        
        "name": "John Doe",  
        "weight": 70,        
        "age": 30,           
        "height": 1.7,       
        "phone": 342391,     
        "address": "myAdress"
    }                        
    

