Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get continent name from country using pycountry

How to convert continent name from country name using pycountry. I have a list of country like this

country = ['India', 'Australia', ....]

And I want to get continent name from it like.

continent = ['Asia', 'Australia', ....]

like image 218
VIBHU BAROT Avatar asked Apr 29 '19 19:04

VIBHU BAROT


1 Answers

Looking at the documentation, would something like this do the trick?:

country_alpha2_to_continent_code()

It converts country code(eg: NO, SE, ES) to continent name.

If first you need to acquire the country code you could use:

country_name_to_country_alpha2(cn_name, cn_name_format="default")

to get the country code from country name.

Full example:

import pycountry_convert as pc

country_code = pc.country_name_to_country_alpha2("China", cn_name_format="default")
print(country_code)
continent_name = pc.country_alpha2_to_continent_code(country_code)
print(continent_name)
like image 54
Oliver H. D. Avatar answered Sep 19 '22 12:09

Oliver H. D.