UnicodeEncodeError: 'charmap' codec can't encode character '\u2010': character maps to <undefined>

Python 3.xSeleniumEncodingUtf 8

Python 3.x Problem Overview


I keep getting UnicodeEncodeError when trying to print a 'Á' that I get from a website requested using selenium in python 3.4.

I already defined at the top of my .py file

# -*- coding: utf-8 -*-

the def is something like this:

from selenium import webdriver

b = webdriver.Firefox()
b.get('http://fisica.uniandes.edu.co/personal/profesores-de-planta')
dataProf = b.find_elements_by_css_selector('td[width="508"]')
for dato in dataProf:
        print(datos.text)

and the exception:

Traceback (most recent call last):
  File "C:/Users/Andres/Desktop/scrap/scrap.py", line 444, in <module>
    dar_p_fisica()
  File "C:/Users/Andres/Desktop/scrap/scrap.py", line 390, in dar_p_fisica
    print(datos.text) #.encode().decode('ascii', 'ignore')
  File "C:\Python34\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2010' in position 173: character maps to <undefined>

thanks in advance

Python 3.x Solutions


Solution 1 - Python 3.x

Already figured it out. As it is noted in this answer, the encoding error doesnt come from python, but from the encoding that the console is using. So the way to fix it is to run the command (in windows):

chcp 65001

that sets the encoding to UTF-8 and then run the program again. Or if working on pycharm as I was, go to Settings>Editor>File Encodings and set the IDE and Project encodings accondingly.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndr&#233;s Fern&#225;ndezView Question on Stackoverflow
Solution 1 - Python 3.xAndrés FernándezView Answer on Stackoverflow