Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 892
Andrés Fernández Avatar asked Sep 03 '15 18:09

Andrés Fernández


People also ask

How do I fix UnicodeEncodeError in Python?

Only a limited number of Unicode characters are mapped to strings. Thus, any character that is not-represented / mapped will cause the encoding to fail and raise UnicodeEncodeError. To avoid this error use the encode( utf-8 ) and decode( utf-8 ) functions accordingly in your code.

What is Charmap codec in Python?

The Python "UnicodeEncodeError: 'charmap' codec can't encode characters in position" occurs when we use an incorrect codec to encode a string to bytes. To solve the error, specify the correct encoding when opening the file or encoding the string, e.g. utf-8 . Here is an example of how the error occurs.


1 Answers

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.

like image 61
Andrés Fernández Avatar answered Sep 21 '22 11:09

Andrés Fernández