Posts: 4
Threads: 2
Joined: Aug 2018
Hi!
Does anyone have any idea why countryinfo package returns the following charmap error message when calling an CountryInfo object?
1 2 3 4 5 6 7 |
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>
|
Posts: 566
Threads: 10
Joined: Apr 2017
Sep-14-2018, 11:09 AM
(This post was last modified: Sep-14-2018, 11:10 AM by volcano63.)
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.
Posts: 7,324
Threads: 123
Joined: Sep 2016
Open countryinfo.py in ....\site-packages\countryinfo folder.
Line 30 add encoding to open:
1 |
country_info = json.load( open (file_path, encoding = 'utf-8' ))
|
Save change.
1 2 3 4 5 6 7 |
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.
Posts: 4
Threads: 2
Joined: Aug 2018
Sep-16-2018, 09:00 AM
(This post was last modified: Sep-16-2018, 09:00 AM by mkaru.)
PyCharm console:
1 2 |
>>> 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?
Posts: 7,324
Threads: 123
Joined: Sep 2016
Sep-16-2018, 10:14 AM
(This post was last modified: Sep-16-2018, 10:14 AM by snippsat.)
(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?
1 2 3 4 5 |
country_info = json.load( open (file_path))
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.
1 |
pip uninstall countryinfo
|
Download countryinfo-0.1.0-py3-none-any
Then:
1 |
pip install countryinfo - 0.1 . 0 - py3 - none - any .whl
|
Posts: 4
Threads: 2
Joined: Aug 2018
I made changes in countryinfo.py file but no positive result:
1 2 3 4 5 6 7 8 9 10 11 |
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>
|
Posts: 7,324
Threads: 123
Joined: Sep 2016
Sep-16-2018, 02:58 PM
(This post was last modified: Sep-16-2018, 02:59 PM by snippsat.)
I think you have misunderstand.
1 |
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.
1 2 3 4 5 6 7 8 9 10 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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.
|