2013-12-17 66 views
1

我使用BeautifulSoup解析一些html,Spyder作为我的编辑器(这两种工具都很棒!)。该代码运行正常的Spyder的,但是当我试图执行从终端的.py文件,我得到一个错误:UnicodeEncodeError - 在Spyder中工作,但不是从终端执行时

file = open('index.html','r') 
soup = BeautifulSoup(file) 
html = soup.prettify() 
file1 = open('index.html', 'wb') 
file1.write(html) 

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in position 5632: ordinal not in range(128) 

我在Linux服务器上运行的openSUSE,使用zypper的安装Spyder的。 有没有人有任何建议这个问题可能是什么? 非常感谢。

回答

1

这是因为,因为输出结果(即其写入文件)之前,必须先对其进行编码:

file1.write(html.encode('utf-8')) 

看到每一个文件都有一个属性file.encoding。引用文档:

file.encoding

The encoding that this file uses. When Unicode strings are written to a file, they will be converted to byte strings using this encoding. In addition, when the file is connected to a terminal, the attribute gives the encoding that the terminal is likely to use (that information might be incorrect if the user has misconfigured the terminal). The attribute is read-only and may not be present on all file-like objects. It may also be None, in which case the file uses the system default encoding for converting Unicode strings.

查看最后一句话? soup.prettify返回一个Unicode对象,并给出这个错误,我敢肯定你使用Python 2.7,因为它的sys.getdefaultencoding()ascii

希望这会有所帮助!

相关问题