Process is coming out of for loop for IJson Iteams

75 views Asked by At

I am using python 3.9 and trying :

with open(file, 'r') as fl:  
    val = ijson.items(fl, '<my_key>.item', use_float=True)
    for i in val:
        print(i)

After some time print statement is not printing anything on jupyter console, but that jupyte cell still run for a very long time.

Is it like, even if I parse specific elements, ijson scan complete file from start to end?, if YES, how can i restrict this behaviour(if it is possible).

Note: Instead of content->print am writing content->into some file, I can see file contents are not changing after some time, but process keeps running. I have tried all sorts of closing file operations etc. Nothing work so far.

Thanks in advance.

1

There are 1 answers

3
Rodrigo Tobar On

The only way to break out of the ijson iteration is to break it yourself (i.e., actually break from the for loop). This is because, as you suggest, ijson goes through reads input files fully. This in turn is because your path (<my_key>.item) could appear again in your file after the initial set of results you are seeing (keys are not required to be unique in JSON).