gpt4 book ai didi

python - 如何在 Python 中处理这个 JSON 文件?

转载 作者:太空宇宙 更新时间:2023-11-04 02:11:51 25 4
gpt4 key购买 nike

我试图从下面的 JSON 文件中提取“点”部分,但发生了错误:

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
}

但它只适用于“句点”部分!
谁能提供一些帮助?

最佳答案

对于提供的 JSON 文件,我们可以提取点部分如下:

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']

关于python - 如何在 Python 中处理这个 JSON 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53584933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com