By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,035 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,035 IT Pros & Developers. It's quick & easy.

utf8 silly question

P: n/a
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...
--
Catalin Constantin
Bounce Software
http://www.bounce-software.com
http://www.cabanova.ro

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Salut, Catalin

You can first convert your c string to unicode, and in the process
specify an encoding that understands non-ASCII characters (if you don't
specify an encoding, it will try to use your default, which is most
likely ASCII, and you'll get the error you mentioned.). In the
following example, I specified 'iso-8859-1' as the encoding.

Then you can utf8-encode the c string via the codecs module.

Here's a snippet of code (note the error when I don't specify a
non-default unicode encoding):

Python 2.4 (#1, Nov 30 2004, 16:42:53)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
c = unicode(chr(169)+" some text") Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa9 in position 0:
ordinal not in range(128) c = unicode(chr(169)+" some text", 'iso-8859-1')
print c some text import codecs
print codecs.encode(c, 'utf-8')

© some text

Jul 19 '05 #2

P: n/a
Grig Gheorghiu wrote:
import codecs
print codecs.encode(c, 'utf-8')


© some text


Or simply:

py> print c.encode('utf-8')
© some text
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.