1

I want the user to input a ISO-3166-1 alpha-2 code.

country_code = input("Please enter a country's two-letter code (e.g. FR, US) :")

I then want to translate the code into the country's name:

FR -> France
US -> United States of America

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

How can I do this ?

Josh Ackland
  • 603
  • 4
  • 19
Akyna
  • 67
  • 1
  • 8
  • Using a dict to translate an abbreviation to the full word -- this is *much* easier. However, you still haven't done the expected research. This is a common technique, which you should look up on this site, rather than posting a question. – Prune Mar 24 '21 at 20:26
  • I didn't find good tagg for search, i find all document about Google Translator but that's not really what i need, do you have any documentation ? I have update my topic i hope he is now better ? – Akyna Mar 24 '21 at 20:29
  • Here's the opposite question: [How to convert country names to ISO 3166-1 alpha-2 values, using python](https://stackoverflow.com/q/16253060/4518341) – wjandrea Mar 24 '21 at 20:30
  • Thanks a lots that's exactly what i need. – Akyna Mar 24 '21 at 20:33

3 Answers3

2

You can try the module pycountry, in the command line run pip install pycountry, and then in your code:

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

Yuval.R
  • 1,182
  • 4
  • 15
1

You could use country_converter, install with pip install country_converter.
This is around 50kb compared to pycountry being around 10mb.

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="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 .

TOE703
  • 21
  • 6
0

This is referred to as "internationalization", and the Python module for doing this is gettext. For example:

In order to prepare your code for I18N, you need to look at all the strings in your files. Any string that needs to be translated should be marked by wrapping it in _('...') — that is, a call to the 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.

Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65