2016-10-22 23 views
2

我只是做一個拉一個數據庫表的下降,並試圖讀取到蟒蛇像這樣:Vertica的Python讀物結果拋出的UnicodeDecodeError

with query(full_query_string) as cur: arr = cur.fetchall()

這從fetchall()產生以下錯誤:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 4: invalid continuation byte

如果我select *我得到這個錯誤,而如果我限制在較小的行數,我沒有得到這個錯誤。我試着在這個SO帖子UnicodeDecodeError, invalid continuation byte之後用幾個編碼付款,但是他們都沒有做到這一點。在一個大型的數據庫表中,我不知道編碼如何出錯,最有效的方法是什麼?此外,沒有具體的行是必須的,但我寧願得到所有的行,而不是任何有這種編碼問題的行。

+0

你確定編碼不是拉丁語?你是如何處理編碼的? –

回答

4

嘿,我知道這是一個超級晚的答案,但在嘗試調試一個類似的問題,我發現,Vertica的 - 蟒蛇README此修復程序:

雖然Vertica的預計VARCHAR處理存儲爲UTF-8編碼,有時 無效字符串進入數據庫。您可以指定如何處理使用unicode_error連接選項讀取這些字符的 。 中採用相同的值作爲unicode的類型 (https://docs.python.org/2/library/functions.html#unicode

嘗試改變「unicode_error」在您的連接鍵從strict PARAMS要麼replaceignore

cur = vertica_python.Connection({..., 'unicode_error': 'strict'}).cursor() 
cur.execute(r"SELECT E'\xC2'") cur.fetchone() 
# caught 'utf8' codec can't decode byte 0xc2 in position 0: unexpected end of data 

cur = vertica_python.Connection({..., 'unicode_error': 'replace'}).cursor() 
cur.execute(r"SELECT E'\xC2'") cur.fetchone() 
# � 

cur = vertica_python.Connection({..., 'unicode_error': 'ignore'}).cursor() 
cur.execute(r"SELECT E'\xC2'") cur.fetchone() 
# 
相關問題