Python

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

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 49: character maps to - Python, someone asked me to explain?

COUNTRY INFO PROGRAM:

I want to retrieve the country information using the 'countryinfo' library. I have installed countryinfo python package using pip.

pip install countryinfo

CODE:

To get country information and print basic country information  using python, import CountryInfo,
from countryinfo import CountryInfo

def get_country_info(country_name):
    try:
        country = CountryInfo(country_name)
        return {
        'capital': country.capital(),
        "Region": country.region(),
        "Currencies": country.currencies(),
        "Languages": country.languages(),
    }
    except Exception as e:
        print(f"Error: {e}")
    
if __name__ == "__main__":
    country_name = input("Enter the country name: ")
    country_details = get_country_info(country_name)
    print(country_details)

I got this following error while executing a python program. 

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

UnicodeDecodeError

SOLUTION:

This problem is related to the character encoding. To handle this issue use encoding utf-8.

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

VIDEO GUIDE:

Post your comments / questions