Python Forum
Countryinfo package charmap error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Countryinfo package charmap error
#1
Hi!

Does anyone have any idea why countryinfo package returns the following charmap error message when calling an CountryInfo object?

from countryinfo import CountryInfo
country = CountryInfo('France')
country.region()

File "...\AppData\Local\Programs\Python\Python37\lib\encodings\cp1257.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0xa1 in position 47: character maps to <undefined>
Reply
#2
Try to set encoding to either UTF-8 or UTF-16 - take a look at this PEP

PS Looks like your default encoding is CP1257
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Open countryinfo.py in ....\site-packages\countryinfo folder.
Line 30 add encoding to open:
country_info = json.load(open(file_path, encoding='utf-8'))
Save change.
E:\div_code\new
λ ptpython
>>> from countryinfo import CountryInfo

>>> country = CountryInfo('France')
>>> country.capital()
'Paris'
I guess he has not testet on Windows,if not specify encoding it will sometimes on Windows fall back to charmap CP1257 encoding.
You can post this in issus Repo countryinfo.
Reply
#4
PyCharm console:

>>> sys.getdefaultencoding()
'utf-8'
So, why does it complain about CP1257 encoding? Any idea to use some other countryinfo packages to find information for a specified country?
Reply
#5
(Sep-16-2018, 09:00 AM)mkaru Wrote: So, why does it complain about CP1257 encoding?
Yes the default encoding is utf-8,but when it try open the .json files it guess wrong.
(Sep-16-2018, 09:00 AM)mkaru Wrote: Any idea to use some other countryinfo packages to find information for a specified country?
Why don't you make change that i postet to line 30?
# Orginal
country_info = json.load(open(file_path))

# Fix
country_info = json.load(open(file_path, encoding='utf-8'))
Then the package work fine.

I can re build it for you with the fix.
pip uninstall countryinfo
Download countryinfo-0.1.0-py3-none-any
Then:
pip install countryinfo-0.1.0-py3-none-any.whl
Reply
#6
I made changes in countryinfo.py file but no positive result:

SyntaxError: invalid syntax
country = CountryInfo('France')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "...\site-packages\countryinfo\countryinfo.py", line 30, in __init__
    country_info = json.load(open(file_path, encoding='utf-8'))
  File "...\Python\Python37\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "...\Python\Python37\lib\encodings\cp1257.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0xa1 in position 47: character maps to <undefined>
Reply
#7
I think you have misunderstand.
File "...\site-packages\countryinfo\countryinfo.py"
This should point to your root python installation,i just use ...\site-packages\countryinfo to point the file.
... should be where your Python 3.7 is installed.

Here with change to line 30 done.
C:\Users\Tom
λ python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from countryinfo import CountryInfo
>>>
>>> country = CountryInfo('France')
>>> country.capital()
'Paris'
>>> exit()
Now i remove encoding='utf-8' from line 30.
C:\Users\Tom
λ python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from countryinfo import CountryInfo
>>>
>>> country = CountryInfo('France')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\python37\lib\site-packages\countryinfo\countryinfo.py", line 30, in __init__
    country_info = json.load(open(file_path))
  File "C:\python37\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\python37\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 49: character maps to <undefined>
You see it point to my python installation File "C:\python37\lib\site-packages\countryinfo\countryinfo.py"
Also i made change to countryinfo.py in this location.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  qpython package error Creepy 4 947 Jul-06-2023, 05:58 PM
Last Post: Creepy
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 34: character Melcu54 7 18,313 Sep-26-2022, 10:09 AM
Last Post: Melcu54
  countryinfo package error MarcusAurelius 4 2,773 Mar-20-2021, 05:42 PM
Last Post: deanhystad
  error with netcdf4 package damdam 0 1,793 Nov-18-2020, 06:44 PM
Last Post: damdam
  'charmap' codec louis216 4 19,989 Jun-30-2020, 06:25 AM
Last Post: louis216
  Importing module from a package results in import error goghvv 2 2,342 Mar-27-2020, 07:13 PM
Last Post: goghvv
  Beginner - simple package installation error mefeng2008 0 1,699 Mar-13-2020, 09:17 AM
Last Post: mefeng2008
  Error importing package julio2000 3 3,709 Jan-26-2020, 06:15 PM
Last Post: buran
  Error in importing package in pycharm fullstop 0 2,315 Dec-12-2019, 04:03 PM
Last Post: fullstop
  error using geoGen package from GITHUB hknarahari 4 3,226 Sep-17-2019, 06:59 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020