我希望用户输入一个ISO-3166-1 alpha-2代码.
country_code = input("Please enter a country's two-letter code (e.g. FR, US) :")然后我想把代码翻译成这个国家的名字:
FR -> France
US -> United States of Americahttps://laendercode.net/en/2-letter-list.html
我该怎么做?
发布于 2021-03-24 20:34:26
您可以在命令行中运行pycountry,然后在代码中尝试模块pip install pycountry:
import pycountry
country_code = input(" Please choose a contry tagg (like fr, us...) :")
country = pycountry.countries.get(alpha_2=country_code)
country_name = country.name有关更多信息,请参见这
发布于 2021-03-24 20:21:57
这被称为“国际化”,用于此操作的Python模块是gettext。例如:
为了为I18N编写代码,您需要查看文件中的所有字符串。任何需要翻译的字符串都应该通过在_(‘.’)中包装它来标记--也就是说,调用函数_()。例如:
filename = 'mylog.txt'
message = _('writing a log message')
with open(filename, 'w') as fp:
fp.write(message)这是一个复杂的主题,这一页将给你很多的信息开始。
发布于 2021-10-15 15:04:29
您可以使用country_converter,用pip install country_converter安装。
这是约50 to,而pycountry是约10毫巴。
转换很容易,如下所示:
import country_converter as coco
input_country = input(" Please choose a contry tagg (like fr, us...) :")
converted_country = coco.convert(names=input_country, to="name")
print(converted_country)names可以是单数的,也可以是可以混合输入的列表。names="fr"names=["fr", "USA", "826", 276, "China"]文档中列出了许多分类方案,它们可以作为输入或转换为。
https://stackoverflow.com/questions/66788711
复制相似问题