Answer a question

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', ....]

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)
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐