2015-08-14 64 views
0

我試圖將一些數據從Google Cloud SQL數據庫導出到Excel文件,使用Python xlsxwriter,webapp2,appengine在延期任務中。UnicodeDecodeError while cursor.fetchall()

要寫入的數據必須從數據庫中檢索。

執行查詢時正常,但當我嘗試從查詢獲取數據或者使用cursor.fetchall()或通過遍歷光標它是扔以下錯誤:

UnicodeDecodeError錯誤: 'UTF8' 編解碼器不能在4位進行解碼字節0x9f:無效起始字節

的堆棧跟蹤是:

for row in cursor: 
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/storage/speckle/python/api/rdbms.py", line 689, in fetchone 
self._FetchMoreRows() 
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/storage/speckle/python/api/rdbms.py", line 606, in _FetchMoreRows 
self._DoExec(request) 
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/storage/speckle/python/api/rdbms.py", line 448, in _DoExec 
return self._HandleResult(response.result) 
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/storage/speckle/python/api/rdbms.py", line 487, in _HandleResult 
new_rows = self._GetRows(result) 
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/storage/speckle/python/api/rdbms.py", line 532, in _GetRows 
tuple_proto.values[value_index])) 
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/storage/speckle/python/api/rdbms.py", line 402, in _DecodeVariable 
return converter(value) 
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/storage/speckle/python/api/converters.py", line 126, in Str2Unicode 
return unicode(arg, 'utf-8') 
UnicodeDecodeError: 'utf8' codec can't decode byte 0x9f in position 4: invalid start byte 

如果我嘗試使用MySQLdb而不是rdbms在本地運行它,則相同的代碼有效。

數據中可能存在一些編碼問題,但在寫入文件時會出現這種問題。

我試圖找到一些可能已損壞但無法找到的數據。

回答

0

您沒有以utf8編碼的文本。它的編碼是什麼?如果是拉丁文1,9F代表Ÿ;這有道理嗎?用十六進制9f查找行;讓我們看看上下文。