10

I am creating a certain function that can give me information regarding the ISS (International Space Station) and a given location (in decimal coordinates) that can vary, depending on the input. But when I use this:

print(ubicacion.raw['address']['country'],",",ubicacion.raw['address']['city'])

It works, but for certain countries, and for example when I try with the coordinates Canberra, it displays the following info:

Corinna Street, Phillip, District of Woden Valley, Australian Capital Territory, 2606, Australia

and since it doesn't provide the city, when I use the key "city", I obviously get an error, since it doesn't exist in that list.

So one solution that I got in my mind was that since at least I will always get the country, maybe I could use another function, that based on the country, I could get the capital city, which is the one I need and it exists, I used "CountryInfo" (from countryinfo import CountryInfo). The problem is, that when I try to use it, I get the following error:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 49: character maps to

I have already searched for similar questions and many people suggest to specify the encoding, but it seems to not work with geopy. since I tried this:

countryinfo=CountryInfo(country,encoding="utf8") 

and I got this error:

TypeError: init() got an unexpected keyword argument 'encoding'

Gsk
  • 2,929
  • 5
  • 22
  • 29
Felipe
  • 140
  • 1
  • 1
  • 9

2 Answers2

25

Referenced from: https://python-forum.io/Thread-Countryinfo-package-charmap-error

from @snippsat's answer Sep-14-2018, 11:37 AM

Open countryinfo.py in ..Lib\site-packages\countryinfo folder. Change the line to:

country_info = json.load(open(file_path, encoding='utf-8'))

It works for me.

Mark K
  • 8,767
  • 14
  • 58
  • 118
2
from countryinfo import CountryInfo

country = CountryInfo('Singapore')
country.capital()
# returns string
'Singapore'

country.capital() will get you the capital in a string format

Amine Mchayaa
  • 104
  • 11
  • Well it doesn't work for me, I get the error from the title. – Felipe Jun 04 '19 at 16:18
  • UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 49: character maps to – Felipe Jun 04 '19 at 16:23
  • Try to at least read my post before commenting the first thing that appears in Google when you search for "CountryInfo Python"... – Felipe Jun 04 '19 at 16:33