在列表中找不到导致程序压碎的元素

发布于 2025-01-18 06:53:38 字数 10241 浏览 2 评论 0 原文

我有一个 .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文件。这个想法是,当用户通过终端或命令行传递参数时,应该检查它,并且如果等于 - 它将在该密钥下打印所有正式的国家名称。由密钥我的意思是 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中编写了同一项目,使用可选的链接更容易。它可以让我返回翻译密钥,而不支持第一个国家/代码>,但是程序将搜索并返回实际出现的元素,而不是压碎。 这是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代码在未在其中一个翻译中找到关键元素后不要压碎吗?

I have a .json file, but because its too big I will only paste part of it:

[
{
    "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"
        }
    }
}]

Then I have a following python code to open and load this .json file. The idea is that when user pass a parameter via terminal or command line- program should check it and if its equal to one of the keys- it will print all the official country names under that key. By keys I mean ces, deu,cym, which are attributes of translation.
Here is the code I wrote:

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

It is working fine for keys that are appear in every translation, but as you can see, cym key is not appearing in the first translations, that's why when I execute python3 main.py cym it returns "Translation key is not supported". I wrote the same project in Javascript before and it was easier there by using Optional Chaining. It would allow me to return Translation key is not supported for the first country, but instead of crushing, program would go search and return those elements that actually appear.
This is the part of Javascript code:

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

Can anyone help me modify this python code not to crush after not finding key element in one of the translations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再让梦枯萎 2025-01-25 06:53:38

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

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

Instead of replacing the translations dictionary each time, merge them using the update() method.

official_keys = {}
for c in countries:
    official_keys.setdefault(c['name']['official'], {}).update(c['translations'])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文