首页 > 解决方案 > 如何打印json的dict的第一个单词?

问题描述

I am trying to read a json and want the output in excel so am trying to put dict in tabular format in excel.

**Code**

    f = open(filename, 'r') #open json file
        data = json.loads(f.read())    #load json
        for s in data['quiz']:     #finding 'quiz type in Json
            Quiz_Type = s     
            print(Quiz_Type)

数据

{
    "quiz": {
        "sport": {
            "q1": {
                "question": "Which one is correct team name in NBA?",
                "options": [
                    "New York Bulls",
                    "Los Angeles Kings",
                    "Golden State Warriros",
                    "Huston Rocket"
                ],
                "answer": "Huston Rocket"
            }
        },
        "maths": {
            "q1": {
                "question": "5 + 7 = ?",
                "options": [
                    "10",
                    "11",
                    "12",
                    "13"
                ],
                "answer": "12"
            },
            "q2": {
                "question": "12 - 8 = ?",
                "options": [
                    "1",
                    "2",
                    "3",
                    "4"
                ],
                "answer": "4"
            }
        }
    }
}



i want print(Quiz_Type) o\p as Sport only..
but instead im getting Sport and Math

我得到了 o/p:- 数学 NBA 中哪一个是正确的球队名称?纽约公牛队||洛杉矶国王队||金州勇士队||休斯顿火箭队

数学 5 + 7 = ? 10||11||12||13 12 12 - 8 = ? 1||2||3||4 4

我需要运动来代替数学第一

标签: pythonjsonprinting

解决方案


你会想使用像 xlsxwriter 这样的库来写回 excel 格式。该库具有全面的文档:

https://xlsxwriter.readthedocs.io/


推荐阅读