0
此代碼對solr索引進行http調用。Python 3,UnicodeEncodeError解碼設置爲忽略
query_uri = prop.solr_base_uri + "?q=" + query + "&wt=json&indent=true"
with urllib.request.urlopen(query_uri) as response:
data = response.read()
#data is bytes
data_str=data.decode('utf-8', 'ignore')
print(data_str)
print語句拋出:
UnicodeEncodeError: 'charmap' codec can't encode character '\u2715' in position 149273: character maps to undefined
我以爲解碼( 'UTF-8', '忽略')應該忽略非UTF-8字符,離開它的結果?它是如何在打印語句中有UnicodeEncodeError的?我如何處理無法用Unicode編碼的字符?謝謝!
也爲什麼:data_str = data.decode('ascii','ignore')工作正常? –
我認爲這可能是我的pycharm控制檯出現問題。我試圖打印到一個文件。同樣的錯誤。 'file = open(「/ temp/test.txt」,「w」) file.write(data_str)' –