[Vobject] I can't write cal to a file

Jeffrey Harris jeffrey at osafoundation.org
Fri Jul 13 11:39:45 CDT 2007


Hi Didier,

> I just found the solution by using module codecs !
>     import codecs
>     def write(self):
>         f = codecs.open(self.file, encoding='utf-8', mode='wb')
>         stream = self.cal.serialize()
>         f.write(stream)
>         f.close()

Yup, you need to encode Unicode, so the above is the right approach.

Note that there's a shortcut available, serialize can be handed a stream
directly, so this also works (and uses less memory):

import codecs
def write(self):
    f = codecs.open(self.file, encoding='utf-8', mode='wb')
    self.cal.serialize(f)
    f.close()

or, better, if you're using Python 2.5, use a context manager to close
the file:

from __future__ import with_statement
import codecs
def write(self):
    with codecs.open(self.file, encoding='utf-8', mode='wb') as f:
        self.cal.serialize(f)

Sincerely,
Jeffrey


More information about the VObject mailing list