cursor.fetchall()时的UnicodeDecodeError

时间:2015-08-14 12:09:29

标签: python mysql google-app-engine encoding webapp2

我试图在延迟任务中使用Python xlsxwriter, webapp2,appengine 将一些数据从Google Cloud SQL数据库导出到excel文件。

必须从数据库中检索要写入的数据。

查询正常运行但是当我尝试使用 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在本地运行它,则相同的代码可以工作。

数据中可能存在一些编码问题,但在写入文件时应该会出现这种问题。

我尝试找到一些可能已损坏但无法找到的数据。

1 个答案:

答案 0 :(得分:0)

您没有以utf8编码的文字。编码的是什么?如果是latin1,则9F代表Ÿ;那会有意义吗?找到带有十六进制9f的行;让我们看看背景。