Wednesday, December 28, 2016

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.