A Simple Python Question (APIs and Excel)
Your boss has asked you to make a spreadsheet of every country in the world and their capital cities. There are over 200 countries in the world. So, you are going to use a Python program to create the Excel spreadsheet.
The World Bank API can provide a list of countries and their capital cities via this URL: http://api.worldbank.org/v2/country?format=json&per_page=400. This response provides a list of countries, but it also includes continents and regions. You can tell the difference because a country has a capitalCity string, while a continent or a region's capitalCity is an empty string.
(JSON formatter is helpful to see the structure of the response, https://jsonformatter.curiousconcept.com/ or Firefox browser, or install a Chrome browser extension like JSON Formatter)
Write a program that:
- Uses requests to fetch the list from the World Bank and convert the JSON into a Python object.
- For each list item in the response:
- Try to extract each country name and capital city from the response.
- Ignore any list entries that are not countries - there are entries for continents and regions, as well as countries. Continents and regions have an empty string for the capitalCity. Only countries have a text value for the capitalCity key in the response.
- Uses openpyxl to write the names and capital cities to a spreadsheet. The country names should be in column A, and their capital cities in column B.
So, the start of your spreadsheet will look like this:
A B
1 Aruba Oranjestad
2 Afghanistan Kabul
3 Angola Luanda
4 Albania Tirane
To submit: your Python program and the Excel spreadsheet that it created.