Unicode is confusing me

D

Dick Moores

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],
.. . .
$B=E(B $BMW(B $B@-(B===============================
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Why is Python telling me variable is local not global? 3
Unicode 2
Unicode 20
python3 Unicode is slow 1
unicode 7
print() and unicode strings (python 3.1) 12
Thinking Unicode 0
unicode bit me 20

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top