473,385 Members | 1,267 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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],
.. . .
$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
Feb 10 '07 #1
0 1117

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Matt Price | last post by:
Hello, I'm a python (& xml, & unicode!) newbie working on an interface to a bibliographic reference server (refdb); I'm running into some encoding problems & am ifnding the plethora of tools a...
6
by: S. | last post by:
if in my website i am using the sgml { notation, is it accurate to say to my users that the site uses unicode or that it requires unicode? is there a mathematical formula to calculate a unicode...
2
by: Neil Schemenauer | last post by:
python-dev@python.org.] The PEP has been rewritten based on a suggestion by Guido to change str() rather than adding a new built-in function. Based on my testing, I believe the idea is...
1
by: willie | last post by:
>willie wrote: wrote:
2
by: willie | last post by:
John Machin: # I'll go ahead and concede defeat since you appear to be on the # verge of a heart attack :) # I can see that I lack clarity so I don't blame you. # By UTF-8 string, I mean...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.