简体   繁体   English

如何根据 ISO-3166-1 alpha-2 代码获取国家/地区名称?

[英]How do I get the country name based on the ISO-3166-1 alpha-2 code?

I want to translate text from variable in python.我想从 python 中的变量翻译文本。

country = input(" Please choose a contry tagg (like fr, us...) :")

I want to translate the text user input such as:我想翻译文本用户输入,例如:

fr = France
us = USA

https://laendercode.net/fr/2-letter-list.html https://laendercode.net/fr/2-letter-list.html

And I want to translate this into the valid country like: France, USA, etc.我想把它翻译成有效的国家,比如:法国、美国等。

How can I do this?我怎样才能做到这一点?

You can try the module pycountry , in the command line run pip install pycountry , and then in your code:您可以尝试模块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

For more info see this有关更多信息,请参阅

This is referred to as "internationalization", and the Python module for doing this is gettext .这被称为“国际化”,用于执行此操作的 Python 模块是gettext For example:例如:

In order to prepare your code for I18N, you need to look at all the strings in your files.为了为 I18N 准备代码,您需要查看文件中的所有字符串。 Any string that needs to be translated should be marked by wrapping it in _('...') — that is, a call to the function _().任何需要翻译的字符串都应通过将其包装在 _('...') 中进行标记——即调用 function _()。 For example:例如:

filename = 'mylog.txt'
message = _('writing a log message')
with open(filename, 'w') as fp:
    fp.write(message)

This is a complex subject and that page will give you a lot of information to start with.这是一个复杂的主题,该页面将为您提供大量信息。

You could use country_converter , install with pip install country_converter .您可以使用country_converter ,安装pip install country_converter
This is around 50kb compared to pycountry being around 10mb.pycountry大约 10mb 相比,这大约是 50kb。

Conversion is easy with something like:转换很容易,例如:

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 can be singular or a list which can be mixed input names可以是单数或可以混合输入的列表
    • names="fr"
    • names=["fr", "USA", "826", 276, "China"]

There are a lot of classification schemes listed in the documentation that can be taken as inputs or converted to.文档中列出了许多分类方案,可以作为输入或转换为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用PYCOUNTRY将ISO 3166-1 alpha-2转换为国家名称 - Use PYCOUNTRY to convert ISO 3166-1 alpha-2 to Country Name 如何使用 python 将国家名称转换为 ISO 3166-1 alpha-2 值 - How to convert country names to ISO 3166-1 alpha-2 values, using python 是否可以让 Plotly choropleth 使用 ISO 3166-1 alpha-2 国家代码而不是 ISO 3166-1 alpha-3 来映射国家? - Is it possible to make Plotly choropleth use ISO 3166-1 alpha-2 country codes instead of ISO 3166-1 alpha-3 to map countries? 如何将 IOC 国家代码转换为国家名称? - How do I convert an IOC country code to country name? 如何从 Django 应用程序中的 ISO 国家代码获取货币符号? - How to get currency symbol from ISO country code in Django application? 国家名称来自字典中的ISO短代码,如何处理非ascii字符 - Country name from ISO short code in dictionary, how to deal with non-ascii chars 从python中的国家代码获取国家/地区名称? - Get country name from Country code in python? 如何以我之前在代码中创建的变量的名称保存 dataframe(代码中看到的 oldest_id 和 iso_data) - How do I save a dataframe in the name of a variable I created earlier in the code (oldest_id and iso_data as seen in the code) 如何在没有外部 API 的情况下通过坐标获取国家名称? - how can I get country name by coordinates without external API? 如何使用 Alpha Vantage API 获得当前价格 - How do I get just the current price with Alpha Vantage API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM