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

问题描述 投票:0回答:1

我有一个

.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
之一,它将打印该键下的所有官方国家/地区名称。我所说的键是指
ces
deu
cym
,它们是
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 parameter 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
时它返回“不支持翻译键”。我之前用 Javascript 编写了相同的项目,通过使用可选链接更容易。它允许我返回第一个国家/地区的
Translation key is not supported
,但程序不会破坏,而是会搜索并返回实际出现的那些元素。 这是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代码,以免在其中一个翻译中找不到关键元素后被压垮吗?

javascript python json command-line-interface
1个回答
1
投票

不要每次都替换翻译词典,而是使用

update()
方法合并它们。

official_keys = {}
for c in countries:
    official_keys.setdefault(c['name']['official'], {}).update(c['translations'])
© www.soinside.com 2019 - 2024. All rights reserved.