Python Forum
Encoding issue for the console output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Encoding issue for the console output
#1
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!
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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.
Reply
#4
Can't say anything else. Never used Mac. I am on Linux machine.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Thanks, I found out that python3 does not have this unicode issue... changed to python3
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Encoding Issue in eyed3 giddyhead 2 1,788 Jul-22-2022, 02:03 AM
Last Post: giddyhead
  multithreading issue with output mr_byte31 4 3,147 Sep-11-2019, 12:04 PM
Last Post: stullis
  Output issue twinpiques 6 3,094 Jul-29-2019, 11:24 PM
Last Post: Yoriz
  Issue with text encoding ans smtplib.SMTP.sendmail() JustSomeUsername383 1 4,109 Jul-23-2019, 03:15 PM
Last Post: JustSomeUsername383
  Save console output to text file (like a log) koticphreak 9 29,110 Jun-02-2019, 07:45 PM
Last Post: snippsat
  print console output to a csv deep_logic 3 4,183 Feb-21-2019, 06:41 PM
Last Post: deep_logic
  re.finditer issue, output is blank anna 1 2,335 Feb-07-2019, 10:41 AM
Last Post: stranac
  Paramiko output printing issue anna 3 15,951 Feb-06-2018, 08:34 AM
Last Post: anna
  unexpected text file output <_io.TextIOWrapper… encoding='cp1252'> sleepylog 3 12,719 Aug-20-2017, 12:06 PM
Last Post: sparkz_alot
  Encoding issue while running Sysinternals Autorunsc via Python subprocess JChris 0 3,427 Jun-13-2017, 12:01 AM
Last Post: JChris

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020