'ascii' codec can't encode character u'\xf3'
xslom03 at vse.cz
Tue Aug 17 08:17:45 CEST 2004
i had similar errors:
Traceback (most recent call last):
File "/home/martin/skripty/accounts.py", line 125, in ?
File "/home/martin/skripty/accounts.py", line 119, in main
File "/home/martin/skripty/accounts.py", line 94, in print_accounts
print str(i).encode("utf-8", "replace")
UnicodeEncodeError: 'ascii' codec can't encode characters in position
151-152: ordinal not in range(128)
- - - -
the solution seems to be:
0. string is not in unicode encoding (assumption)
1. before printing out, convert the string to unicode
2. when printing, convert to whatever charset you like
though i dont understand much why (ive solved it a minute ago :) the
code should be:
str = "any nonunicode string"
print unicode(str).encode("iso-8859-2", "replace")
1. why the string is not in unicode can have several reasons -- i guess:
- does ogg stores tags in unicode?
- you have parsed an xml file with encoding attribute set (that
is what i do)
2. "replace" parameter in encode causes non-printable chars to be
replaced with '?' (you can use "ignore" or strict", see your python
3. the above will work _only_ _if_ the 'str' encoding is "iso-8859-2" --
a funny thing -- first line of code converts from unknown (but the
programmer must know it) to unicode and the second one converts it back
from unicode to unknown (now the programmer tells that secret to python
4. i would like to know from any python expert whether/why/why not:
* my assumptions are right
* why is that behaviour? -- if you search google you get
thousands of errors like this -- with no proper solutions i must add
* is there an easier portable way (no sitecustomize.py changes)
to do it
* i was looking in site.py and there is deleted the
sys.setdefaultencoding() function, but from the comments i do
not know why -- you know it? why is user not allowed to change the
default encoding? it seems reasonable to me if he/she could do that.
More information about the Python-list