在列表中找不到导致程序崩溃的元素

Not finding element in a list causing program to crush

我有.json文件,但是因为太大我只贴一部分:

[
{
    "name": {
        "common": "Aruba",
        "official": "Aruba",
        "native": {
            "nld": {
                "official": "Aruba",
                "common": "Aruba"
            },
            "pap": {
                "official": "Aruba",
                "common": "Aruba"
            }
        }
    },
    "tld": [
        ".aw"
    ],
    "cca2": "AW",
    "ccn3": "533",
    "cca3": "ABW",
    "cioc": "ARU",
    "independent": false,
    "status": "officially-assigned",
    "unMember": false,
    "currencies": {
        "AWG": {
            "name": "Aruban florin",
            "symbol": "\u0192"
        }
    },
    "idd": {
        "root": "+2",
        "suffixes": [
            "97"
        ]
    },
    "capital": [
        "Oranjestad"
    ],
    "altSpellings": [
        "AW"
    ],
    "region": "Americas",
    "subregion": "Caribbean",
    "languages": {
        "nld": "Dutch",
        "pap": "Papiamento"
    },
    "translations": {
        "ces": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "deu": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "est": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "fin": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "fra": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "hrv": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "hun": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "ita": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "jpn": {
            "official": "\u30a2\u30eb\u30d0",
            "common": "\u30a2\u30eb\u30d0"
        },
        "kor": {
            "official": "\uc544\ub8e8\ubc14",
            "common": "\uc544\ub8e8\ubc14"
        },
        "nld": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "per": {
            "official": "\u0622\u0631\u0648\u0628\u0627",
            "common": "\u0622\u0631\u0648\u0628\u0627"
        },
        "pol": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "por": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "rus": {
            "official": "\u0410\u0440\u0443\u0431\u0430",
            "common": "\u0410\u0440\u0443\u0431\u0430"
        },
        "slk": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "spa": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "swe": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "urd": {
            "official": "\u0627\u0631\u0648\u0628\u0627",
            "common": "\u0627\u0631\u0648\u0628\u0627"
        },
        "zho": {
            "official": "\u963f\u9c81\u5df4",
            "common": "\u963f\u9c81\u5df4"
        }
    },
    "latlng": [
        12.5,
        -69.96666666
    ],
    "landlocked": false,
    "borders": [],
    "area": 180,
    "flag": "\ud83c\udde6\ud83c\uddfc",
    "demonyms": {
        "eng": {
            "f": "Aruban",
            "m": "Aruban"
        },
        "fra": {
            "f": "Arubaise",
            "m": "Arubais"
        }
    }
},
{
    "name": {
        "common": "Afghanistan",
        "official": "Islamic Republic of Afghanistan",
        "native": {
            "prs": {
                "official": "\u062c\u0645\u0647\u0648\u0631\u06cc \u0627\u0633\u0644\u0627\u0645\u06cc \u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646",
                "common": "\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646"
            },
            "pus": {
                "official": "\u062f \u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646 \u0627\u0633\u0644\u0627\u0645\u064a \u062c\u0645\u0647\u0648\u0631\u06cc\u062a",
                "common": "\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646"
            },
            "tuk": {
                "official": "Owganystan Yslam Respublikasy",
                "common": "Owganystan"
            }
        }
    },
    "tld": [
        ".af"
    ],
    "cca2": "AF",
    "ccn3": "004",
    "cca3": "AFG",
    "cioc": "AFG",
    "independent": true,
    "status": "officially-assigned",
    "unMember": true,
    "currencies": {
        "AFN": {
            "name": "Afghan afghani",
            "symbol": "\u060b"
        }
    },
    "idd": {
        "root": "+9",
        "suffixes": [
            "3"
        ]
    },
    "capital": [
        "Kabul"
    ],
    "altSpellings": [
        "AF",
        "Af\u0121\u0101nist\u0101n"
    ],
    "region": "Asia",
    "subregion": "Southern Asia",
    "languages": {
        "prs": "Dari",
        "pus": "Pashto",
        "tuk": "Turkmen"
    },
    "translations": {
        "ces": {
            "official": "Afgh\u00e1nsk\u00e1 isl\u00e1msk\u00e1 republika",
            "common": "Afgh\u00e1nist\u00e1n"
        },
        "cym": {
            "official": "Gweriniaeth Islamaidd Affganistan",
            "common": "Affganistan"
        },
        "deu": {
            "official": "Islamische Republik Afghanistan",
            "common": "Afghanistan"
        },
        "est": {
            "official": "Afganistani Islamivabariik",
            "common": "Afganistan"
        },
        "fin": {
            "official": "Afganistanin islamilainen tasavalta",
            "common": "Afganistan"
        },
        "fra": {
            "official": "R\u00e9publique islamique d'Afghanistan",
            "common": "Afghanistan"
        },
        "hrv": {
            "official": "Islamska Republika Afganistan",
            "common": "Afganistan"
        },
        "hun": {
            "official": "Afganiszt\u00e1ni Iszl\u00e1m K\u00f6zt\u00e1rsas\u00e1g",
            "common": "Afganiszt\u00e1n"
        },
        "ita": {
            "official": "Repubblica islamica dell'Afghanistan",
            "common": "Afghanistan"
        },
        "jpn": {
            "official": "\u30a2\u30d5\u30ac\u30cb\u30b9\u30bf\u30f3\u00b7\u30a4\u30b9\u30e9\u30e0\u5171\u548c\u56fd",
            "common": "\u30a2\u30d5\u30ac\u30cb\u30b9\u30bf\u30f3"
        },
        "kor": {
            "official": "\uc544\ud504\uac00\ub2c8\uc2a4\ud0c4 \uc774\uc2ac\ub78c \uacf5\ud654\uad6d",
            "common": "\uc544\ud504\uac00\ub2c8\uc2a4\ud0c4"
        },
        "nld": {
            "official": "Islamitische Republiek Afghanistan",
            "common": "Afghanistan"
        },
        "per": {
            "official": "\u062c\u0645\u0647\u0648\u0631\u06cc \u0627\u0633\u0644\u0627\u0645\u06cc \u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646",
            "common": "\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646"
        },
        "pol": {
            "official": "Islamska Republika Afganistanu",
            "common": "Afganistan"
        },
        "por": {
            "official": "Rep\u00fablica Isl\u00e2mica do Afeganist\u00e3o",
            "common": "Afeganist\u00e3o"
        },
        "rus": {
            "official": "\u0418\u0441\u043b\u0430\u043c\u0441\u043a\u0430\u044f \u0420\u0435\u0441\u043f\u0443\u0431\u043b\u0438\u043a\u0430 \u0410\u0444\u0433\u0430\u043d\u0438\u0441\u0442\u0430\u043d",
            "common": "\u0410\u0444\u0433\u0430\u043d\u0438\u0441\u0442\u0430\u043d"
        },
        "slk": {
            "official": "Afg\u00e1nsky islamsk\u00fd \u0161t\u00e1t",
            "common": "Afganistan"
        },
        "spa": {
            "official": "Rep\u00fablica Isl\u00e1mica de Afganist\u00e1n",
            "common": "Afganist\u00e1n"
        },
        "swe": {
            "official": "Islamiska republiken Afghanistan",
            "common": "Afghanistan"
        },
        "urd": {
            "official": "\u0627\u0633\u0644\u0627\u0645\u06cc \u062c\u0645\u06c1\u0648\u0631\u06cc\u06c1 \u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646",
            "common": "\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646"
        },
        "zho": {
            "official": "\u963f\u5bcc\u6c57\u4f0a\u65af\u5170\u5171\u548c\u56fd",
            "common": "\u963f\u5bcc\u6c57"
        }
    },
    "latlng": [
        33,
        65
    ],
    "landlocked": true,
    "borders": [
        "IRN",
        "PAK",
        "TKM",
        "UZB",
        "TJK",
        "CHN"
    ],
    "area": 652230,
    "flag": "\ud83c\udde6\ud83c\uddeb",
    "demonyms": {
        "eng": {
            "f": "Afghan",
            "m": "Afghan"
        },
        "fra": {
            "f": "Afghane",
            "m": "Afghan"
        }
    }
}]

然后我有下面的 python 代码来打开和加载这个 .json 文件。这个想法是,当用户通过终端或命令行传递参数时 - 程序应该检查它,如果它等于 keys 之一 - 它会打印该键下的所有官方国家名称。我所说的键是指 cesdeucym,它们是 translation 的属性。 这是我写的代码:

import json
import sys
from sys import argv
import json

#access .json file and load it's insides
with open('countries.json') as f:
countries = json.load(f)

#create a list of translation keys from .json file
official_keys = {}
for c in countries:
  official_keys[c['name']['official']] = c['translations']

if len(sys.argv) == 1 or len(sys.argv) > 2:
   print("Incorrect parameter")
   sys.exit()
else:
   key = argv[1]

#return countries names if CLI pAarameter matches one of the keys
for official, keys  in official_keys.items():
  if (key in keys):
      print(keys[key]["official"])
  else : 
      print ("Translation key is not supported")
      break

它适用于出现在每个翻译中的键,但正如您所看到的,cym 键没有出现在第一个翻译中,这就是为什么当我执行 python3 main.py cym 它 returns “不支持翻译键”。我之前在 Javascript 中编写了相同的项目,并且通过使用可选链接在那里更容易。这将允许我 return Translation key is not supported 用于第一个国家,但程序将搜索并 return 那些实际出现的元素,而不是粉碎。 这是 Javascript 代码的一部分:

try {
for (const {translations} of data) {
  const key = translations[enteredKey]?.official;
  if (key) {
    console.log(key);
  }
}
} catch (error) {
console.log('Cannot translate variable');
}

任何人都可以帮我修改此 python 代码,以便在其中一个翻译中找不到关键元素后不要崩溃?

不是每次都替换翻译词典,而是使用 update() 方法合并它们。

official_keys = {}
for c in countries:
    official_keys.setdefault(c['name']['official'], {}).update(c['translations'])