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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to