Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd like to provide a solution. The key thought is to "try" to decode: if it fails, give it more feed, otherwise use the offset information to prepare next decoding.</p> <p>However the current json module can't tolerate SPACE in head of string to be decoded, so I have to strip them off.</p> <pre><code>import sys import json def iterload(file): buffer = "" dec = json.JSONDecoder() for line in file: buffer = buffer.strip(" \n\r\t") + line.strip(" \n\r\t") while(True): try: r = dec.raw_decode(buffer) except: break yield r[0] buffer = buffer[r[1]:].strip(" \n\r\t") for o in iterload(sys.stdin): print("Working on a", type(o), o) </code></pre> <p>========================= I have tested for several txt files, and it works fine. (in1.txt)</p> <pre><code>{"foo": ["bar", "baz"] } 1 2 [ ] 4 {"foo1": ["bar1", {"foo2":{"A":1, "B":3}, "DDD":4}] } 5 6 </code></pre> <p>(in2.txt)</p> <pre><code>{"foo" : ["bar", "baz"] } 1 2 [ ] 4 5 6 </code></pre> <p>(in.txt, your initial)</p> <pre><code>{"foo": ["bar", "baz"]} 1 2 [] 4 5 6 </code></pre> <p>(output for Benedict's testcase)</p> <pre><code>python test.py &lt; in.txt ('Working on a', &lt;type 'list'&gt;, [u'hello']) ('Working on a', &lt;type 'dict'&gt;, {u'goodbye': 1}) ('Working on a', &lt;type 'int'&gt;, 1) ('Working on a', &lt;type 'int'&gt;, 2) ('Working on a', &lt;type 'dict'&gt;, {}) ('Working on a', &lt;type 'int'&gt;, 2) ('Working on a', &lt;type 'int'&gt;, 9) ('Working on a', &lt;type 'int'&gt;, 78) ('Working on a', &lt;type 'int'&gt;, 4) ('Working on a', &lt;type 'int'&gt;, 5) ('Working on a', &lt;type 'dict'&gt;, {u'animals': [u'dog', u'lots of mice', u'cat']}) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload