Python Forum
Encoding issue for the console output - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Encoding issue for the console output (/thread-4779.html)



Encoding issue for the console output - ted_chou12 - Sep-08-2017

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!


RE: Encoding issue for the console output - wavic - Sep-08-2017

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.


RE: Encoding issue for the console output - ted_chou12 - Sep-08-2017

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.


RE: Encoding issue for the console output - wavic - Sep-08-2017

Can't say anything else. Never used Mac. I am on Linux machine.


RE: Encoding issue for the console output - ted_chou12 - Sep-08-2017

Thanks, I found out that python3 does not have this unicode issue... changed to python3