4

Pythonには2つの辞書があると言います-

辞書1

mydict1 = { 
        "Person" :
            {
                "FName"    : "Rakesh",
                "LName"    : "Roshan",
                "Gender"   : "Male",
                "Status"   : "Married",
                "Age"      : "60",
                "Children" :
                    [
                        {
                            "Fname"    : "Hrithik",
                            "Lname"    : "Roshan",
                            "Gender"   : "Male",
                            "Status"   : "Married",
                            "Children" : ["Akram", "Kamal"],
                        },
                        {
                            "Fname"    : "Pinky",
                            "Lname"    : "Roshan",
                            "Gender"   : "Female",
                            "Status"   : "Married",
                            "Children" : ["Suzan", "Tina", "Parveen"]
                        }
                    ],
                "Movies" : 
                    {
                        "The Last Day" :
                            {
                                "Year" : 1990,
                                "Director" : "Mr. Kapoor"
                            },
                        "Monster" :
                            {
                                "Year" : 1991,
                                "Director" : "Mr. Khanna"
                            }
                    }
             }
    }

辞書2

mydict2 = {
        "Person" :
            {
                "FName"    : "Rakesh",
                "LName"    : "Roshan",
                "Gender"   : "Male",
                "Status"   : "Married",
                "Children" :
                    [
                        {
                            "Fname"    : "Hrithik",
                            "Lname"    : "Losan",
                            "Gender"   : "Male",
                            "Status"   : "Married",
                            "Children" : ["Akram", "Ajamal"],
                        },
                        {
                            "Fname"    : "Pinky",
                            "Lname"    : "Roshan",
                            "Gender"   : "Female",
                            "Status"   : "Married",
                            "Children" : ["Suzan", "Tina"]
                        }
                    ]
             }
    }

2 つの辞書を比較し、以下のようにレポート形式の違いを出力したい -

MISMATCH 1
==========
MATCH DICT KEY : Person >> Children >> LName
EXPECTED  : Roshan
ACUTAL    : Losan


MISMATCH 2
==========
MATCH LIST ITEM : Person >> Children >> Children
EXPECTED        : Kamal
ACTUAL          : Ajamal


MISMATCH 3
==========
MATCH LIST ITEM : Person >> Children >> Children
EXPECTED        : Parveen
ACTUAL          : NOT_FOUND

MISMATCH 4
==========
MATCH DICT KEY  : Person >> Age
EXPECTED        : 60
ACTUAL          : NOT_FOUND 

MISMATCH 5
==========
MATCH DICT KEY  : Person >> Movies
EXPECTED        : { Movies : {<COMPLETE DICT>} } 
ACTUAL          : NOT_FOUND

辞書形式できれいな出力が得られない datadiff という Python モジュールを試しました。レポートを生成するには、辞書をたどって「+」「-」キーを見つける必要があります。ディクショナリが複雑すぎると、トラバースが難しくなります。

4

1 に答える 1