Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to retrieve file position during JSON parsing with ijson in Python?

I’m using the ijson library in Python to parse a large JSON file and I need to find the position in the file where specific data is located. I want to use file.tell() to get the current position of the file reader during the parsing process. But it’s only giving me the length of the file.

from ijson import parse
with open('file','r') as f:
 for a, b, c in parse(f):
  print(f.tell())

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ijson.parse is using buffered reads of the source file:

>>> help(ijson.parse)
Help on function parse in module ijson.common:

parse(source, buf_size=65536, **config)

If your file is smaller than 64K it will look like f.tell() is returning the file size.

If you use parse(f, buf_size=1) the f.tell() should be accurate, but parsing will likely be slower.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading