2013-12-17 90 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

希望這會有所幫助!

相關問題