gpt4 book ai didi

python-3.x - MySQLDB 'utf-8' 编解码器问题无法解码字节 0x92

转载 作者:行者123 更新时间:2023-12-03 18:21:36 25 4
gpt4 key购买 nike

我在使用 MySQLdb 运行查询时遇到问题。我有以下错误信息

': 'utf-8' codec can't decode byte 0x92 in position 2: invalid start byte

我过去能够在没有编码问题的情况下运行此查询,因此我不知道我的问题从何而来。我所做的唯一更改是在我的笔记本电脑上下载 Postgres 并删除(意外地)一个 db.sqlite3 文件。但我不明白为什么这会影响我的查询编码,特别是因为我没有任何特殊字符。运行一个更简单的查询可以正常工作,从我同事的笔记本电脑上运行这个查询也可以。

查询如下:

SLEEPERS_QUERY = """
select * from candidates"""
import MySQLdb as db


with SSHTunnelForwarder(
ssh_address_or_host = host,
ssh_port = 22,
ssh_username = ssh_username,
ssh_pkey = ssh_private_key,
remote_bind_address = (rds, 3306), # 3306 = mysql port
) as server:
server.start()
print('Connected to the SSH server')

while True:
try:
conn = db.connect(
host = localhost,
port = server.local_bind_port,
user = user,
passwd = password,
db = database
)
print('Connected to the database server')
break

except: pass
df = pd.read_sql_query(query,conn)

return df

最佳答案

我找到了一个解决方案:我的默认编码从 latin1 更改为 utf8。

我必须添加 charset = 'latin1'

conn = db.connect(
host = localhost,
port = server.local_bind_port,
user = user,
passwd = password,
db = database,
charset = 'latin1'
)

关于python-3.x - MySQLDB 'utf-8' 编解码器问题无法解码字节 0x92,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60385406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com