[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [FYI] Python 2.0 problems!!!



On Sat, Feb 01, 2003 at 09:53:14PM +0000, Walid Shaari wrote:
> Dear Arabeyes,
> 
> I have installed mlterm 2.6.3, and was trying to print Arabic Strings
> using python Interpreter for example just a simple letter as in:
> 
> 	print u'\u0631\u0632\u0633'
> 
> It didn't seem to work at first as it was giving me this error :
> 
> 	UnicodeError: ASCII encoding error: ordinal not in range(128)
> 
> Through trail and Errors, and some googling I managed to solve this with
> the following code :
> 
> 	import sys
> 	reload (sys)
> 	sys.setdefaultencoding('utf-8')
> 	print u'\u0631\u0632\u0633'
> 
> it prints on the right hand side correctly as I wanted!
> 
> So where is my Question?
> 
> I don't really have a question yet, but noting the above could be added
> to either the Arabic howto, or even by archiving it some-one else can
> benefit.
> 
> Other than that, please advise me if there is a better Arabeyesiation
> solution for a pythonist ;-)

I am not sure what you are trying to do (and I guess you're not sure what
your question is ;) -- but to clarify some things:

Generally you don't print Unicode strings, instead you encode them (hopefully
in UTF-8). For instance, using Unicode strings is useful if you want
to do certain analysis (I find it useful in some testing with
Duali) so your libraries don't necessarily have to care about the specific
encoding.

e.g.
str =  u'\u0631\u0632\u0633
print str.encode('utf-8')

Now you are outputting in UTF-8 encoding. Likewise, you can convert a
given encoding to unicode.

str2 = str.encode('utf-8')
str3 = unicode(str, 'utf-8')

Now we have str3 back to the same unicode as the original 'str'. Pretty
nifty ;)

later
-- 
-------------------------------------------------------
| Mohammed Elzubeir    | Visit us at:                 |
|                      |  http://www.arabeyes.org/    |
| Arabeyes Project     | Homepage:                    |
| Unix the 'right' way |  http://fakkir.net/~elzubeir/|
-------------------------------------------------------
---
Was I helpful? Let others know:
http://svcs.affero.net/rm.php?r=elzubeir

Attachment: pgp00001.pgp
Description: PGP signature