如何在Python中处理此JSON文件?

2024-03-29 12:21:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从下面的JSON文件中提取“points”部分,但出现了一个错误:

with open("file1.json", "r") as read_file:
    data = json.load(read_file)
data['points']

这是我的错误:

KeyError  
Traceback (most recent call last)  
<ipython-input-127-a97424859b31> in <module>()  
----> 1 data['points']  
KeyError: 'points' 

这是我的JSON:

{
   "duration": 2727,
   "height": 756,
   "periods": [
      {
         "actions": [
            {
               "action": "createShape",
               "actionID": 217,
               "endTime": 2727,
               "points": [
                  {
                     "t": 2014,
                     "x": 715,
                     "y": 367.5
                  },
                  {
                     "t": 2049,
                     "x": 714.5,
                     "y": 367.5
                  },
                  {
                     "t": 2064,
                     "x": 713.5,
                     "y": 367.5
                  },
                  {
                     "t": 2096,
                     "x": 711,
                     "y": 367.5
                  },
                  {
                     "t": 2115,
                     "x": 707.5,
                     "y": 368
                  },
                  {
                     "t": 2132,
                     "x": 705,
                     "y": 368
                  },
                  {
                     "t": 2148,
                     "x": 702,
                     "y": 368.5
                  },
                  {
                     "t": 2162,
                     "x": 700,
                     "y": 368.5
                  },
                  {
                     "t": 2176,
                     "x": 697,
                     "y": 369
                  },
                  {
                     "t": 2198,
                     "x": 693,
                     "y": 369
                  },
                  {
                     "t": 2215,
                     "x": 690,
                     "y": 369
                  },
                  {
                     "t": 2231,
                     "x": 686,
                     "y": 368.5
                  },
                  {
                     "t": 2248,
                     "x": 681,
                     "y": 368.5
                  },
                  {
                     "t": 2265,
                     "x": 676.5,
                     "y": 368.5
                  },
                  {
                     "t": 2276,
                     "x": 674.5,
                     "y": 368.5
                  },
                  {
                     "t": 2298,
                     "x": 671,
                     "y": 369
                  },
                  {
                     "t": 2315,
                     "x": 667.5,
                     "y": 370
                  },
                  {
                     "t": 2333,
                     "x": 664.5,
                     "y": 370
                  },
                  {
                     "t": 2348,
                     "x": 660.5,
                     "y": 370
                  },
                  {
                     "t": 2365,
                     "x": 656.5,
                     "y": 370
                  },
                  {
                     "t": 2382,
                     "x": 653,
                     "y": 370.5
                  },
                  {
                     "t": 2399,
                     "x": 650,
                     "y": 370.5
                  },
                  {
                     "t": 2415,
                     "x": 647.5,
                     "y": 370.5
                  },
                  {
                     "t": 2432,
                     "x": 644,
                     "y": 370.5
                  },
                  {
                     "t": 2449,
                     "x": 640.5,
                     "y": 370.5
                  },
                  {
                     "t": 2460,
                     "x": 638.5,
                     "y": 370.5
                  },
                  {
                     "t": 2478,
                     "x": 635,
                     "y": 370
                  },
                  {
                     "t": 2498,
                     "x": 630,
                     "y": 370
                  },
                  {
                     "t": 2515,
                     "x": 628,
                     "y": 370
                  },
                  {
                     "t": 2531,
                     "x": 625.5,
                     "y": 370
                  },
                  {
                     "t": 2549,
                     "x": 623,
                     "y": 370
                  },
                  {
                     "t": 2565,
                     "x": 620,
                     "y": 370
                  },
                  {
                     "t": 2584,
                     "x": 618,
                     "y": 370
                  },
                  {
                     "t": 2594,
                     "x": 617,
                     "y": 370
                  },
                  {
                     "t": 2609,
                     "x": 615,
                     "y": 370
                  },
                  {
                     "t": 2626,
                     "x": 613.5,
                     "y": 370
                  },
                  {
                     "t": 2643,
                     "x": 612.5,
                     "y": 370
                  },
                  {
                     "t": 2697,
                     "x": 614.5,
                     "y": 370
                  },
                  {
                     "t": 2705,
                     "x": 615,
                     "y": 370
                  },
                  {
                     "t": 2727,
                     "x": 616,
                     "y": 370.5
                  }
               ],
               "startTime": 2014,
               "strokeColor": "#F8A602",
               "strokeWidth": 4,
               "type": "pen"
            }
         ],
         "endTime": 2727,
         "id": 1,
         "startTime": 0,
         "type": "recording"
      }
   ],
   "platform": "iOS",
   "width": 1344
}

但它只适用于'期间'部分!
有人能帮忙吗?你知道吗


Tags: 文件jsonreaddatatype错误withopen
1条回答
网友
1楼 · 发布于 2024-03-29 12:21:48

对于提供的JSON文件,我们可以按如下方式提取point部分:

import json

with open("file1.json", "r") as read_file:
    data = json.load(read_file)
points = data['periods'][0]['actions'][0]['points'] 

让我们看看我们是如何一步一步实现的:

# get periods section
periods_lst = data['periods']
# periods is a list to drill down into the data structure 
# we have to specify the element of that list by index
# that list contain only 1 element with index 0
periods = periods_lst[0]
# get actions section
actions_lst = periods['actions']
# actions is a list which also contains only 1 element
actions = actions_lst[0]
# get points
points_lst = actions['points']
# get necessary point by index, e.g.
point0 = points_lst[0]
point8 = points_lst[8]
# get the list of points in one line
points = data['periods'][0]['actions'][0]['points'] 

相关问题 更多 >