utf8 silly question

Qiangning Hong hongqn at gmail.com
Tue Jun 21 13:14:21 EDT 2005


Catalin Constantin wrote:
> i have the following code:
> 
> c=chr(169)+" some text"
> 
> how can i utf8 encode the variable above ?
> something like in php utf8_encode($var);?!
> 
> chr(169) is the &copy (c) sign !
> 
> 10x for your help !
> 
> p.s.: i tryed using codecs, etc but always get an error message
> like: 'ascii' codec can't decode byte 0xa9 in position 0...

ascii can not handle chr(169), so you need to use some other encoding to
make unicode string first, such as latin-1.

In [1]:c = chr(169) + ' some text'

In [2]:u = unicode(c, 'latin-1')

In [3]:u8 = u.encode('UTF-8')

In [4]:u8
Out[4]:'\xc2\xa9 some text'

In [5]:print u8
© some text

*NOTE*: At the last statement "print u8" I can print a UTF-8 string
directly because my console is UTF-8 encoding.  On your machine the
&copy sign maybe not show.

-- 
Qiangning Hong

 _______________________________
( Do you like "TENDER VITTLES"? )
 -------------------------------
 o     /\  ___  /\
  o   // \/   \/ \\
     ((    O O    ))
      \\ /     \ //
       \/  | |  \/
        |  | |  |
        |  | |  |
        |   o   |
        | |   | |
        |m|   |m|



More information about the Python-list mailing list