Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read multiple dictionaries from a file in python?

I am relatively new to python. I am trying to read an ascii file with multiple dictionaries in it. The file has the following format.

{Key1: value1
 key2: value2
 ...
}
{Key1: value1
 key2: value2
 ...
}
{
...

Every dictionary in the file is a nested dictionary. I am trying to read it as a list of dictionaries. is there any simple way to do this? i have tried the following code but it doesn't seem to work

data = json.load(open('doc.txt'))
like image 559
Rahul Avatar asked Dec 26 '14 15:12

Rahul


1 Answers

Provided the inner elements are valid JSON, the following could work. I dug up the source of simplejson library and modified it to suit your use case. An SSCCE is below.

import re
import simplejson

FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
WHITESPACE = re.compile(r'[ \t\n\r]*', FLAGS)

def grabJSON(s):
    """Takes the largest bite of JSON from the string.
       Returns (object_parsed, remaining_string)
    """
    decoder = simplejson.JSONDecoder()
    obj, end = decoder.raw_decode(s)
    end = WHITESPACE.match(s, end).end()
    return obj, s[end:]

def main():
    with open("out.txt") as f:
        s = f.read()

    while True:
        obj, remaining = grabJSON(s)
        print ">", obj
        s = remaining
        if not remaining.strip():
            break

.. which with some similar JSON in out.txt will output something like:

> {'hello': ['world', 'hell', {'test': 'haha'}]}
> {'hello': ['world', 'hell', {'test': 'haha'}]}
> {'hello': ['world', 'hell', {'test': 'haha'}]}
like image 80
UltraInstinct Avatar answered Sep 23 '22 07:09

UltraInstinct