Solve charmap codec error in Python
Error:
'charmap' codec can't encode character '\u2013' in position 112: character maps to <undefined>
Problem code:
for d in soup.find_all(href=re.compile(url)):
print(d)
It is not an error from Python but it is because of Windows, in my case there was one character which was not getting encoded properly in command prompt (console) so I had to change the encoding to UTF-8. Solution is pretty simple just type following command.
Solution:
J:\>chcp 65001
Active code page: 65001
CHCP changes the active console code page. 65000 code page is encoded as UTF-7 and 65001 code page is encoded as UTF-8.
ref2.: http://ss64.com/nt/chcp.html
This Python error, the charmap codec cannot encode a character, comes from Windows defaulting to a legacy code page that cannot represent certain Unicode characters such as an en dash. The script logic itself is fine.
Writing or printing with an explicit UTF-8 encoding, for example by setting encoding on open or reconfiguring stdout, lets the unsupported characters through.

Comments
Post a Comment