Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Python Programming Language

Unicode is confusing me


As per p. 188 of Python for Dummies, I've created a sitecustomize.py
in  my site-packages directory:

============================
# sitecustomize.py  (see p.188 of Python for Dummies)
import sys
sys.setdefaultencoding('utf-8')
===============================

With that in place, at the interactive prompt this goes well:
===============================

>>> import sys
>>> sys.getdefaultencoding()
'utf-8'
>>> a = [u'\u91cd', u'\u8981', u'\u6027']
>>>for x in range(3):

. . .   print a[x],
. . .
? ? ?

===============================
The 3 CJK characters are printed very nicely.

However, when I put the last 3 lines of that code in a script,

============================
a = [u'\u91cd', u'\u8981', u'\u6027']
for x in range(3):
    print a[x]
===========================

and redirect the output to a text file, I get
"Traceback (most recent call last):
  File "E:\Python25\dev\Untitled1.py", line 3, in ?
    print a[x]
UnicodeEncodeError: 'ascii' codec can't encode character u'\u91cd' in
position 0: ordinal not in range(128)"

I thought maybe it would help if I made the first line,
# -*- coding: utf-8 -*-

but that made no difference. Got the same error.

Can someone help me understand what's going on?

Thanks,

Dick Moores
--
my configuration:
Win XP Pro SP2
Python 2.5
wxPython 2.8.1.1 Unicode
Python IDE: Ulipad 3.6

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc