Read json file in Python

Asked 2 years ago, Updated 2 years ago, 77 views

When reading numbers from multiple json files, you want to read 345 files, but you can only read 256 files.
I don't know the cause, so please tell me the reason and how to deal with it.

Program Code

import json

i=000000000000000000000000000000000000

for file_name in ["rightleft_{:012d}_keypoints.json".format(i) for i in range (345)]:
    with open(file_name) asf:
        read_data=json.load(f)
        print(read_data['people'][0]['face_keypoints_2d'][21])

US>Error Code
After loading up to 256 files

Traceback (most recent call last):

says

IndexError:list index out of range

The error appears.

I'm a beginner in the program, so it might be basic, but please do it

python python3 json

2022-09-30 21:38

1 Answers

Why don't you enclose each line with try exception?
Check if the file is not valid json data, if there is no data equivalent to ['people'][0]['face_keypoints_2d'][21] and under which line the error occurred.

This post was edited based on @kunif's Comment and posted as Community Wiki.This post was edited based on @kunif's Comment and posted as Community Wiki.


2022-09-30 21:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.