Vertica Python读取结果抛出UnicodeDecodeE

2024-03-28 20:21:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我只是在下拉一个数据库表,并尝试将其读入python,如下所示:

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 *我得到这个错误,而如果我限制在一小部分行,我就不会得到这个错误。我试着用下面的一些编码来支付,但是没有一个能做到。在一个大的db表中,我不知道编码是如何出错的,处理这个问题最有效的方法是什么?另外,没有特定的行是必须的,但是我宁愿得到所有的行,而不是那些有编码问题的行。在


Tags: 数据库编码stringas错误withbytequery
1条回答
网友
1楼 · 发布于 2024-03-28 20:21:44

嘿,我知道这是一个非常晚的答案,但在尝试调试类似的问题时,我从vertica python自述文件中找到了以下修复:

While Vertica expects varchars stored to be UTF-8 encoded, sometimes invalid strings get into the database. You can specify how to handle reading these characters using the unicode_error connection option. This uses the same values as the unicode type (https://docs.python.org/2/library/functions.html#unicode)

尝试将连接参数中的“unicode错误”键从strict更改为replace或{}:

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()
#

相关问题 更多 >