Connection to the database ( Pervasive ).

The base encoding is cp866 . OS - Windows

I can not display the Cyrillic alphabet in the console.

import pyodbc dbfile = pyodbc.connect('DSN=test') cursor = dbfile.cursor() cursor.execute("select NameAccount from account_dbt") row = cursor.fetchall() for l in range(4): t = str(row[l][0]) print(t) 

Result:

 ЋЋЋ ђ®¬ иЄ Ћ¤Ё(Romashka odin) ЋЋЋ ђ®¬ иЄ Ћ¤Ё(Romashka odin) - 2 ЋЋЋ ђ®¬ иЄ Ћ¤Ё(Romashka odin) - 3 ЋЋЋ ђ®¬ иЄ Ћ¤Ё(Romashka odin) - 4 

I tried this:

 t = str(row[l][0]).encode('utf-8') 

Result:

 b'\xd0\x8b\xd0\x8b\xd0\x8b \xd1\x92\xc2\xae\xc2\xac\xc2\xa0\xd0\xb8\xd0\x84\xc2\xa0 \xd0\x8b\xc2\xa4\xd0\x81\xc2\xad(Romashka odin)' b'\xd0\x8b\xd0\x8b\xd0\x8b \xd1\x92\xc2\xae\xc2\xac\xc2\xa0\xd0\xb8\xd0\x84\xc2\xa0 \xd0\x8b\xc2\xa4\xd0\x81\xc2\xad(Romashka odin) - 2' b'\xd0\x8b\xd0\x8b\xd0\x8b \xd1\x92\xc2\xae\xc2\xac\xc2\xa0\xd0\xb8\xd0\x84\xc2\xa0 \xd0\x8b\xc2\xa4\xd0\x81\xc2\xad(Romashka odin) - 3' b'\xd0\x8b\xd0\x8b\xd0\x8b \xd1\x92\xc2\xae\xc2\xac\xc2\xa0\xd0\xb8\xd0\x84\xc2\xa0 \xd0\x8b\xc2\xa4\xd0\x81\xc2\xad(Romashka odin) - 4' 

Article on Habré about encoding also did not clarify.

How to display the text in a readable form?

  • try to specify charset . I do not know how this is interpreted by the pyodbc code, so I would first try to specify utf8 , and if this does not lead to some visual changes, then cp866 (or how it should be correctly named in the operating system you use). - aleksandr barakin
  • @alexander barakin, specified both UTF8 and CP866 when connecting. The result is the same. - Evgeny Bulokhov
  • if you tried, then, it seems to me, it is worth mentioning this explicitly in the text of the question. - aleksandr barakin
  • Windows linux I don’t know about Linux, but Windows has a very peculiar console that has its own accounts with encodings. Try to start to bring to the file to understand when the code is working correctly. After that, torment the console. Dig to the side of the CHCP command. My solution in Python2 was absolutely terrible: stackoverflow.com/questions/878972/… and you can look at this: stackoverflow.com/questions/14109024/… - Petr Abdulin
  • 2
    the answer to the question should not be placed in the question itself. Post it as an answer (so that you can vote, comment, etc.) - jfs

1 answer 1

Decision:

 t = str(row[l][0]).encode('cp1251'), encoding='cp866') 

First, the row row[l][0] translated into a byte string encoded in cp1251, and then into a regular string encoded in cp1251.