Retrieving JSON objects from a text file (using Python)

后端 未结 9 1538
失恋的感觉
失恋的感觉 2020-11-30 04:50

I have thousands of text files containing multiple JSON objects, but unfortunately there is no delimiter between the objects. Objects are stored as dictionaries and some of

相关标签:
9条回答
  • 2020-11-30 05:47

    Why don't you load the file as string, replace all }{ with },{ and surround the whole thing with []? Something like:

    re.sub('\}\s*?\{', '\}, \{', string_read_from_a_file)
    

    Or simple string replace if you are sure you always have }{ without whitespaces in between.

    In case you expect }{ to occur in strings as well, you could also split on }{ and evaluate each fragment with json.load, in case you get an error, the fragment wasn't complete and you have to add the next to the first one and so forth.

    0 讨论(0)
  • 2020-11-30 05:50

    Replace a file with that junk in it:

    $ sed -i -e 's;}{;}, {;g' foo
    

    Do it on the fly in Python:

    junkJson.replace('}{', '}, {')
    
    0 讨论(0)
  • 2020-11-30 05:51

    How about reading through the file incrementing a counter every time a { is found and decrementing it when you come across a }. When your counter reaches 0 you'll know that you've come to the end of the first object so send that through json.load and start counting again. Then just repeat to completion.

    0 讨论(0)
提交回复
热议问题