python从mssql检索非英语文本

2024-03-29 10:50:27 发布

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

我正在尝试使用python pyodbc模块从mssqlserver检索非英语数据。你知道吗

非英语文本如下所示

\u0905\u0939\u092e\u0926\u0928\u0917\u0930 

我正在编写一个API层,在该层中我将以json格式发送相同的文本。我希望数据以如下方式显示

अहमदनगर 

我是否需要在pyodbc中配置一些东西,以便正确显示数据?

我的pyodbc连接字符串是

mssql+pyodbc://user:password@machine/Database?charset=utf8

谢谢 阿尼鲁达


Tags: 模块数据文本apijson格式pyodbcmssqlserver
1条回答
网友
1楼 · 发布于 2024-03-29 10:50:27

确保源文件编码为UTF-8。以下代码适用于我:

# -*- coding: utf-8 -*-
import pyodbc

connStr = (
    r'Driver={SQL Server};' +
    r'Server=127.0.0.1,52865;' +
    r'Database=myDb;' +
    r'Trusted_Connection=Yes;'
    )
db = pyodbc.connect(connStr)
sql = "SELECT word FROM vocabulary WHERE ID=1"
cursor1 = db.execute(sql)
while 1:
    row = cursor1.fetchone()
    if not row:
        break
    print row.word
cursor1.close()
db.close()

注意第一行中的编码提示。pythonshell(IDLE)中显示的结果是

你好

相关问题 更多 >