質問

I am trying to retrieve non-english data from MS SQL Server using python pyodbc module.

The non-english text looks like following

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

I am writting an API layer, in which I will send the same text in json format. I am expecting data to be shown in a way like

अहमदनगर 

Do I need to configure something in pyodbc, so that the data will be shown correctly?

My pyodbc connection string is

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

Thanks Aniruddha

役に立ちましたか?

解決

Make sure that your source file is encoded as UTF-8. The following code works for me:

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

Note the coding hint in the first line. The result that appears in the Python shell (IDLE) is

你好
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top