UnicodeEncodeError in Windows

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Sep 17 21:36:48 CEST 2007

En Mon, 17 Sep 2007 07:38:16 -0300, geoff_ness <geoffness8 at gmail.com>  

> def buildString(warrior):
>         """Build a string from a warrior's stats
>         Returns string for output to warStat."""
>         return "!tr!!td!!id!"+str(warrior.ID)+"!/id!!/td!"+\
>         "!td!"+str(warrior.damage)+"!/td!!td!"+str(warrior.kills)+\
>         "!/td!!td!"+str(warrior.survived)+"!/td!!/tr!"
> This code runs fine on my linux machine, but when I sent the code to a
> friend with python running on windows, he got the following error:
> Traceback (most recent call last):
>  File "C:\Documents and Settings\Administrator\Desktop
> \reparser_014(2)\iotp_alt2.py", line 169, in buildString
>    "!/td!!td!"+str(warrior.survived)+"!/td!!/tr!"
> UnicodeEncodeError: 'ascii' codec can't encode character u'\ufeff' in
> position 0: ordinal not in range(128)
> As I understand it the error is related to the ascii codec being
> unable to cope with the unicode string u'\ufeff'.
> The issue I have is that this error doesn't show up for me - ascii is
> the default encoding for me also. Any thoughts or assistance would be
> welcomed.

Some of those `warrior` attributes is an Unicode object that contains  
characters outside ASCII. str(x) tries to convert to string, using the  
default encoding, and fails. This happens on Windows and Linux too,  
depending on the data.
I've seen that you use codecs.open: you should write Unicode objects to  
the file, not strings, and that would be fine.
Look for some recent posts about this same problem.

Gabriel Genellina

More information about the Python-list mailing list