Created
December 10, 2012 20:44
-
-
Save theonewolf/4253245 to your computer and use it in GitHub Desktop.
parse a collection of packed JSON documents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import json | |
| data = open('database.json').read() | |
| document = True | |
| start = 0 | |
| end = None | |
| while document: | |
| try: | |
| document = json.loads(data[start:end]) | |
| print document | |
| if end > 0: | |
| start = end | |
| end = None | |
| # only final document loads and also has end set as None | |
| elif end == None: | |
| break | |
| except ValueError as e: | |
| # first case: if we encountered a true error | |
| if e.args[0] == 'No JSON object could be decoded': break | |
| # second case: find where error occurs, that is end of current document | |
| else: end = start + int(e.args[0]. | |
| split('column')[1].strip(). | |
| split(' ')[0]) | |
| continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment