如何创建多级json文件,然后从python中读取特定值?

如何创建多级json文件,然后从python中读取特定值?,python,json,python-3.x,Python,Json,Python 3.x,我正在为学习阿拉伯语做一个测验式的项目。我现在需要从代码中删除问题并将它们存储在一个外部文件中——我在思考.JSON。但是,我在从python访问某些问题或部分文件时遇到困难,我不确定这是因为我没有正确格式化.JSON文件,还是因为我的代码有错误。我已经包括了下面两个。最后,我计划上多节课,每节课有3-4个部分,在.JSON文件中有很多问题。目前我只有一节课,分为三个部分,以确保我做的事情是正确的。我在这里看了另一个问题(),但解决方案似乎对我不起作用 如果我现在尝试运行代码,会收到以下错误消息

我正在为学习阿拉伯语做一个测验式的项目。我现在需要从代码中删除问题并将它们存储在一个外部文件中——我在思考.JSON。但是,我在从python访问某些问题或部分文件时遇到困难,我不确定这是因为我没有正确格式化.JSON文件,还是因为我的代码有错误。我已经包括了下面两个。最后,我计划上多节课,每节课有3-4个部分,在.JSON文件中有很多问题。目前我只有一节课,分为三个部分,以确保我做的事情是正确的。我在这里看了另一个问题(),但解决方案似乎对我不起作用

如果我现在尝试运行代码,会收到以下错误消息:

Traceback (most recent call last):
File "C:/LearningArabic/Test Programs/JSON open file test.py", line 10, in <module>
print(data["lesson 1"]["part two"])
KeyError: 'part two'
该行:

for i in data["lesson 1"]["part one"]:
    print (i["question"])
工作并打印第一部分中的所有问题,但在这之后,我得到了前面提到的错误

这是我的.JSON文件:

{"lesson 1":[
    {"part one": [
        {"question": "What is the meaning of 'واد' ?",
        "transliteration": "walid",
        "answer": "boy"
        },
        {"question": "What is the meaning of 'بنت' ?",
        "transliteration": "bint",
        "answer": "girl"
        },
        {"question": "What is the meaning of 'رخل' ?",
        "transliteration": "ragul",
        "answer": "man"
        },
        {"question": "What is the meaning of 'ست' ?",
        "transliteration": "sit",
        "answer": "woman"
        }
    ],
    "part two": [
        {"question": "What is the meaning of '2test1'?",
        "transliteration": "phonix",
        "answer": "21"
        },
        {"question": "What is the meaning of '2test2'?",
        "transliteration": "phonix2",
        "answer": "22"
        }
    ],
    "part three": [
        {"question": "What is the meaning of '3test1'?",
        "transliteration": "phonix",
        "answer": "31"
        },
        {"question": "What is the meaning of '3test2'?",
        "transliteration": "phonix2",
        "answer": "32"
        }
    ]}
]}

以下JSON文件:

{"lesson 1":
    {"part one": [
        {"question": "What is the meaning of 'ست' ?",
        "transliteration": "sit",
        "answer": "woman"
        }
    ],
    "part two": [
    ],
    "part three": [
        {"question": "What is the meaning of '3test2'?",
        "transliteration": "phonix2",
        "answer": "32"
        }
    ]}
}
使用以下python代码:

导入json

with open("test.json", "r", encoding = "utf-8-sig") as read_file:
    data = json.load(read_file)

for i in data["lesson 1"]["part one"]:
    print (i["question"])

for i in data["lesson 1"]["part two"]:
    print (i["answer"])
一切正常<代码>打印(数据[“第一课”][“第二部分”])也可以


您需要删除
“第1课:
后面的方括号,并整理变量的索引

您可以使用嵌套循环像这样解析它:

for k in data["lesson 1"].keys():
    print(data["lesson 1"][k])
    for i in range(len(data["lesson 1"][k])):
        print(data["lesson 1"][k][i]["question"])
输出:

 [{'question': "What is the meaning of 'واد' ?", 'transliteration': 'walid', 'answer': 'boy'}, {'question': "What is the meaning of 'بنت' ?", 'transliteration': 'bint', 'answer': 'girl'}, {'question': "What is the meaning of 'رخل' ?", 'transliteration': 'ragul', 'answer': 'man'}, {'question': "What is the meaning of 'ست' ?", 'transliteration': 'sit', 'answer': 'woman'}]
    What is the meaning of 'واد' ?
    What is the meaning of 'بنت' ?
    What is the meaning of 'رخل' ?
    What is the meaning of 'ست' ?
    [{'question': "What is the meaning of '2test1'?", 'transliteration': 'phonix', 'answer': '21'}, {'question': "What is the meaning of '2test2'?", 'transliteration': 'phonix2', 'answer': '22'}]
    What is the meaning of '2test1'?
    What is the meaning of '2test2'?
    [{'question': "What is the meaning of '3test1'?", 'transliteration': 'phonix', 'answer': '31'}, {'question': "What is the meaning of '3test2'?", 'transliteration': 'phonix2', 'answer': '32'}]
    What is the meaning of '3test1'?
    What is the meaning of '3test2'?

使用jsonl文件怎么样?这样做会很容易。我确实使用了json文件,这是问题的最后一部分。我把它包括在这里,因为我担心可能会出现格式错误,这会给我错误消息。我是说使用jsonl文件,不同于json@piethon26,jsonl fi的好处是什么le vs a json文件?嵌套更少,我想我只是在考虑它,因为jsonl在技术上是json的json,所以每一行都可以是json。我删除了“lesson one”之后的“[”括号,得到了相同的错误。然后,我在最后删除了“]”括号(因此所有括号都将关闭)还是没有运气。我的文件中的代码正确吗?Markus,这对我来说是可行的。我的json.file文件中肯定有格式错误,但我将它简化为每个部分一个问题,然后它就成功了(还删除了括号)。然后我用其他问题重新构建了json.file,现在一切正常。谢谢!Luca,这确实有效,但没有回答我的问题。我的json.file中的格式有问题。不过,这是显示json.file部分的一种很酷的方式。
 [{'question': "What is the meaning of 'واد' ?", 'transliteration': 'walid', 'answer': 'boy'}, {'question': "What is the meaning of 'بنت' ?", 'transliteration': 'bint', 'answer': 'girl'}, {'question': "What is the meaning of 'رخل' ?", 'transliteration': 'ragul', 'answer': 'man'}, {'question': "What is the meaning of 'ست' ?", 'transliteration': 'sit', 'answer': 'woman'}]
    What is the meaning of 'واد' ?
    What is the meaning of 'بنت' ?
    What is the meaning of 'رخل' ?
    What is the meaning of 'ست' ?
    [{'question': "What is the meaning of '2test1'?", 'transliteration': 'phonix', 'answer': '21'}, {'question': "What is the meaning of '2test2'?", 'transliteration': 'phonix2', 'answer': '22'}]
    What is the meaning of '2test1'?
    What is the meaning of '2test2'?
    [{'question': "What is the meaning of '3test1'?", 'transliteration': 'phonix', 'answer': '31'}, {'question': "What is the meaning of '3test2'?", 'transliteration': 'phonix2', 'answer': '32'}]
    What is the meaning of '3test1'?
    What is the meaning of '3test2'?