UnicodeEncodeError:'charmap'编解码器无法编码字符'\\\‐':字符映射到< undefined> [英] UnicodeEncodeError: 'charmap' codec can't encode character '\u2010': character maps to <undefined>
问题描述
我已经在顶部定义了一个Á,我从使用selenium的python 3.4中请求的网站打印出来。我的.py文件
# - * - 编码:utf-8 - * -
def是这样的:
从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]')$ data
print(datos.text)
和例外:
追溯(最近最近通话):
文件C :/Users/Andres/Desktop/scrap/scrap.py,第444行在< module>
dar_p_fisica()
文件C:/Users/Andres/Desktop/scrap/scrap.py,第390行,在dar_p_fisica
print(datos.text)#.encode()中。解码('ascii','ignore')
文件C:\Python34\lib\encodings\cp1252.py,第19行,编码
返回codecs.charmap_encode(输入, self.errors,encoding_table)[0]
UnicodeEncodeError:'charmap'编解码器不能在位置173中编码字符'\\\‐':字符映射到< undefined>
提前感谢
已经弄清楚了。如此答案所述,编码错误不是来自python,而是来自控制台正在使用的编码。所以修复它的方法是运行命令(在Windows中):
chcp 65001
将编码设置为UTF-8,然后再次运行程序。或者如果像我一样工作在pycharm上,请转到设置>编辑器>文件编码,并自动设置IDE和项目编码。
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
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.
这篇关于UnicodeEncodeError:'charmap'编解码器无法编码字符'\\\‐':字符映射到< undefined>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!