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'))
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'}]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With