pandas.read_sql 的 UnicodeDecodeError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40231498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:16:47  来源:igfitidea点击:

UnicodeDecodeError with pandas.read_sql

pythonpandasunicode

提问by spiff

UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 8: character maps to <undefined>

Am seeing teh above error in a simple sql query:

我在一个简单的 sql 查询中看到上述错误:

df = pd.read_sql(query,connection)

I tried the query normally on sql developer and it works perfectly fine.. really stumped here as to how to specify encoding in a read_sql call

我在 sql developer 上正常地尝试了查询,它工作得很好。

Am using Python 3.4 and pandas version 0.14.1

我正在使用 Python 3.4 和 Pandas 版本 0.14.1

Thanks very much!

非常感谢!

回答by Hisham Karam

the proper encoding for your database is iso-8859-1according to oracle docsso When you connect()to your database, pass the charset='iso-8859-1'or encoding='iso-8859-1'try both.

您的数据库的正确编码是iso-8859-1根据 oracle文档,所以当您connect()访问您的数据库时,通过charset='iso-8859-1'encoding='iso-8859-1'尝试两者。

回答by Vlad

Python3.7:

Python3.7:

con = sqlite3.connect(path_to_db)
encoding = "latin1"
con.text_factory = lambda x: str(x, encoding)
# do not preserve non-printable
# con.text_factory = lambda x: str(x, "ascii", errors="ignore")
data = pd.read_sql_query(QUERY, con)

Pydocs on text_factory

text_factory 上的 Pydocs