reload(sys)

Sönmez Kartal rainwatching at gmail.com
Fri Aug 31 08:53:36 EDT 2007


On 31 A ustos, 04:24, Steven Bethard <steven.beth... at gmail.com> wrote:
> Sönmez Kartal wrote:
> > I've had an encoding issue and solved it by
> > "sys.setdefaultencoding('utf-8')"...
>
> > My first try wasn't successful since setdefaultencoding is not named
> > when I imported sys module. After, I import sys module, I needed to
> > write "reload(sys)" also.
>
> > I wonder why we need to call "reload(sys)" to get setdefaultencoding
> > named?
>
> sys.setdefaultencoding is purposely deleted from the sys module after
> it's loaded because you really shouldn't be using it. The reload() call
> restores the deleted attribute.
>
> If you'd like a less brittle solution to your encoding issue, explain
> what the issue was, and people here can probably help you find a better
> solution.
>
> STeVe

I was using the XMLBuilder(xmlbuilder.py). I'm writing XML files as
"f.write(str(xml))". At execution of that line, it gives error with
description, configure your default encoding... My operating system's
default is utf-8, and Emacs' is utf-8 too. Default of XMLBuilder is
utf-8 too. There were some characters interpreter may couldn't print
in ascii. I have tried to replace those characters like (TM) (R)... I
cannot remember them right now, but if necessary I can find them
easily...

This is the part of xmlbuilder.py which raises the error.

        try:
            if self.pretty:
                # tabs are evil, so we will use two spaces
                outstr = self._dom.toprettyxml("
",encoding=self.encoding)
            else:
                outstr = self._dom.toxml(encoding=self.encoding)
        except UnicodeDecodeError:
            sys.stderr.write('Decoding Error: You must configure
default encoding\n')
            sys.exit()

What I can do instead of "import sys; reload(sys);
sys.setdefaultencoding('utf-8')"?

Happy coding




More information about the Python-list mailing list