UnicodeEncodeError in Windows

geoff_ness geoffness8 at gmail.com
Tue Sep 18 06:03:58 EDT 2007


On Sep 18, 7:36 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Mon, 17 Sep 2007 07:38:16 -0300, geoff_ness <geoffne... at gmail.com>
> escribi?:
>
>
>
> > 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

Thanks Gabriel, I hadn't thought about the str() function that way - I
had initially used it to coerce the attributes which are type int to
type str so that I could write them to the output file. I've rewritten
the buildString() function now so that the unicode objects don't get
fed to str(), and apparently windows copes ok with that. I'm still
puzzled as to why python at my end had no problem with it...




More information about the Python-list mailing list