Python Forum

Full Version: Encoding issue for the console output
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import sys
reload(sys)
sys.setdefaultencoding('utf8')

import pyodbc

driver = '{ODBC Driver 13 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password+';CHARSET=SJIS')
#cnxn.setencoding(unicode, encoding='utf-8')
cursor = cnxn.cursor()
cursor.execute("select ...  where q_contract_summary.identifier='" + identifier + "'")
row = cursor.fetchone()
while row:
    sys.stdout.write(str(row[0]).decode('unicode_escape'))
    #print(str(row[0]).decode('utf-8') + " " + unicode(str(row[1]), "utf-8"))
    row = cursor.fetchone()
I see in my console is:
Quote:a?¢a??a?≪a??a??c?°a¢?tedchou-MacBook-Air:~ ted.chou$ python test.py
a?¢a??a?≪a??a??c?°a¢?tedchou-MacBook-Air:~ ted.chou$ python test.py
a?¢a??a?≪a??a??c?°a¢?tedchou-MacBook-Air:~ ted.chou$

I believe that this is the encoding problem, could anyone guide me in the right direction?

Thank you so much!
This is not related to Python but to the tty application. It can't decode properly the folder name. As you see. Change the directory to /home/$USER/ or open another terminal window and should be alright.
Thank you so much! But I am using Mac OS default terminal... I believe that it is able to display UTF-8 properly:
Quote:電子郵件DLP.zip
(外国人・帰国子女向け)日本で働くためのビジネス基礎研修様.pdf
(送付用)【ご参考情報】メール流量確認結果_20170317.xlsx
tedchou-MacBook-Air:Downloads ted.chou$ vim test.abcdefg
tedchou-MacBook-Air:Downloads ted.chou$ python test.py
python: can't open file 'test.py': [Errno 2] No such file or directory
tedchou-MacBook-Air:Downloads ted.chou$ cd ~
tedchou-MacBook-Air:~ ted.chou$ python test.py
a?¢a??a?≪a??a??c?°a¢?tedchou-MacBook-Air:~ ted.chou$ python test.py
a?\xa2a??a?\u226aa??a??c?\xb0a\xa2?tedchou-MacBook-Air:~ ted.chou$ python test.py
a?\xa2a??a?\u226aa??a??c?\xb0a\xa2?tedchou-MacBook-Air:~ ted.chou$
I think it should be alright...
Thanks again.
Can't say anything else. Never used Mac. I am on Linux machine.
Thanks, I found out that python3 does not have this unicode issue... changed to python3