DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Unicode string works in web based Python shell but not on desktop?  (Read 7,579 times)

daddydave

  • test
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 867
    • Participated in NANY Challenge Once
Any idea why I can create a Unicode string in the Python shell on a Silverlight-based Python on the web but not in IDLE or any other Python shell on my desktop? I am using Google IME to type in both cases.

Screenshot - 6_2_2012 , 12_36_31 PM.png


mahesh2k

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,426
    • Participated in NANY Challenge Twice
I am using Python 2.7.3 on Ubuntu 12.04 Precise and it seems to be working for me. I guess it could be windows issue. I used google transliterate for the string.

daddydave

  • test
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 867
    • Participated in NANY Challenge Once
Found the fix and it works.
I notice the indentation is screwed up in stackoverflow -- I put the encoding = 'utf-8' line in line with the try and except preceding it.
This seems to have something to do with the Windows default encoding for stdout not being UTF-8.

Code: (python)
encoding = "ascii"
if sys.platform == 'win32':
    # On Windows, we could use "mbcs". However, to give the user
    # a portable encoding name, we need to find the code page
    try:
        encoding = locale.getdefaultlocale()[1]
        codecs.lookup(encoding)
    except LookupError:
        pass
    encoding = 'utf-8'
« Last Edit: June 03, 2012, 02:53 PM by daddydave »

Original address: https://www.donationcoder.com/forum/index.php?topic=31203.0