gpt4 book ai didi

python - 从 ms sql 迁移到 postgres 非 ascii 字符错误 python

转载 作者:行者123 更新时间:2023-11-29 13:50:20 25 4
gpt4 key购买 nike

我需要创建一个 python 脚本来将数据从 Microsoft SQL Server 迁移到 PostgreSql。问题出现在非 ascii 字符上。在 Microsoft SQL Server 中,我有一个名为 table1 的表,其中包含一个 nvarchar 类型的列,该列存储一个字符

â

我正在使用 pyodbc 检索字符。我的连接字符串是

"ms_sql":{
"DRIVER":"{SQL Server Native Client 11.0}",
"SERVER":"(localdb)\\v11.0",
"DATABASE":"sheshant_database",
"Trusted_Connection":"yes",
"charset":"SQL_Latin1_General_CP1_CI_AS"

ms sql中的排序规则是SQL_Latin1_General_CP1_CI_AS。当我在 Python 中检索数据时,它给出了

u'\xe2'

然后我通过 psycopg2 连接到 postgres,这是我的连接字符串参数

    "postgres":{
"host":"127.0.0.1",
"user":"postgres",
"password":"regards",
"database":"cmots",
"port":"5432"

在 PostgreSQL 中,客户端和服务器的编码分别是 'latin1' 和 'utf8'。我用的是psycopg2中的命令

'insert into table1 values ('+ a +');' // here a is a unicode and a = u'\xe2'

.但是PostgreSQL中存储的数据是

Γ

哪里出错了?

最佳答案

检查 Windows 编码并替换它:

cursor.execute ('insert into table1 values (%s)', a.decode('latin'));

关于python - 从 ms sql 迁移到 postgres 非 ascii 字符错误 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42282156/

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