Bottom Page

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>
Quote
#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.
Quote
#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.
Quote
#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?
Quote
#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
Quote
#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>
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  error using geoGen package from GITHUB hknarahari 4 107 Sep-17-2019, 06:59 AM
Last Post: buran
  charmap codec can't decode byte error with gzipped file in python bluethundr 2 330 Apr-30-2019, 12:26 PM
Last Post: bluethundr
  package(s) error pykdtree zahid990170 0 273 Mar-21-2019, 06:18 PM
Last Post: zahid990170
  UnicodeEncodeError: 'charmap' codec can't encode character '\u2019' in position 23: c kapilan15 2 974 Jan-14-2019, 09:11 PM
Last Post: Gribouillis
  Error while importing my own package giu88 4 569 Sep-19-2018, 12:18 PM
Last Post: giu88
  python charmap codec can't decode byte X in position Y character maps to < undefined> owais 9 21,063 Apr-28-2018, 10:52 PM
Last Post: abadawi
  Exchange2org - Charmap Encode issue r7rajkumar 1 749 Feb-19-2018, 09:37 AM
Last Post: buran
  ibm_db error even though package is installed runnerpaul 6 3,839 Mar-28-2017, 07:22 AM
Last Post: runnerpaul
  unicodeencodeerror 'charmap' codec can't encode characters in position panoss 3 20,820 Jan-28-2017, 03:27 PM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)