An easier way would be to use the codecs module to open the file for
reading or writing in a particular encoding (the system default
encoding is assumed when using open() or file(), which is apparently
different from iso-8859-1 in the case above). I have to deal with
encoded files quite frequently, so I have a class that allows me to
pass an optional encoding value when getting file objects. The
getFile() classmethod that returns a file object looks something like
this (don't forget to import codecs):

    def getFile(self, filename, mode='r', enc=None):
        """Return a file object"""
        f = None
        if enc:
            try:
                f = codecs.open(filename, mode, enc)
            except:
                raise
        else:
            try:
                f = file(filename, mode)
            except:
                raise
        return f
    getFile = classmethod(getFile)


You can also change the encoding used on an open file object. Here's
that method:

    def setFileEncoding(self, fileobj, enc):
        """Set a file obj's encoding"""
        fileobj = codecs.lookup(enc)[-1](fileobj)
    setFileEncoding = classmethod(setFileEncoding)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to