0

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.

Shubham Chauhan
  • 119
  • 2
  • 14

1 Answers1

0

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).

Rodrigo Tobar
  • 569
  • 4
  • 13
  • Thanks, @Rodrigo Tobar.. So if i have more then one elemets for which i want to iterate, am actually reading file that many times, ? – Shubham Chauhan Jul 11 '22 at 16:52
  • I am unable to get control in loop itself, so I am unable do something like, break after 10000 records, irrespective of records , (less or more) – Shubham Chauhan Jul 11 '22 at 16:58
  • If you have to iterate over more than one list you'll have to use a lower-level procedure like `ijson.parse`. – Rodrigo Tobar Jul 13 '22 at 03:03