Search code examples
pythonwindowsunicodeconsolepymssql

Next question about russian encoding, mssql and python


Next question about russian encoding, mssql and python.

I have this simple code:

import pymssql
import codecs
conn=pymssql.connect(host='localhost:1433', user='sa',  password='password', database='TvPgms')
cur = conn.cursor()
cur.execute('SELECT TOP 5 CAST( Name AS nvarchar(400) ), CONVERT(nvarchar(400), idProgram) FROM dbo.Programs')
p=cur.fetchone()
h=p[0]
d=codecs.lookup(h)
print h
conn.close()

I get the error: LookUp Error : Unnown Encoding: ????? ?????? ???????

I cant reed russian varchar filds from MSSQL. But when i just print string in the same code all is ok, it print me normal russian characters. Who know how?

If I truing just print h insted of codecs.lookup than i get no error, but it prints me ???????? ?????????


Solution

  • codecs.lookup takes an encoding name, not some random string, and you probably don't need it here anyway. I think at the moment you cannot reliably print Unicode strings from Python to the Windows console due to deep technical problems. Try writing to a file or using the WriteConsoleW function directly (via ctypes) instead.