2017-04-06 230 views
0
The string that could not be encoded/decoded was: 熔樹脂溫度(℃) 

    'ascii' codec can't encode characters in position 0-4: ordinal not in range(128) 

我想編碼ascii字符,但我得到錯誤。我曾嘗試過如何編碼或解碼ascii編解碼器?

熔樹脂溫度(℃).encode('utf-8') 
熔樹脂溫度(℃).encode('utf8') 
unicode(熔樹脂溫度(℃),'utf-8') 

沒有什麼可行的。

+0

我相信你需要的Unicode沒有ASCII – chbchb55

+0

'.encode( 'UTF-8')'沒有工作?這個對我有用。 – Rishav

+1

我不確定python-2.7是否啓用了unicode,你可能需要python3 – chbchb55

回答

0

要轉換成Unicode:

In [1]: str1 = '熔樹脂溫度(℃)' 

In [2]: print str1 
熔樹脂溫度(℃) 

In [3]: str1 
Out[3]: '\xe7\x86\x94\xe6\xa0\x91\xe8\x84\x82\xe6\xb8\xa9\xe5\xba\xa6(\xe2\x84\x83)' 

In [4]: unicode_str1 = str1.decode('UTF-8') 

In [5]: print unicode_str1 
熔樹脂溫度(℃) 

In [6]: unicode_str1 
Out[6]: u'\u7194\u6811\u8102\u6e29\u5ea6(\u2103)' 
+0

這假定OP運行使用utf-8的python shell。假設這是一個Windows代碼頁? – tdelaney