[Vobject] vCard and iCalendar examples with UTF-8?

Jeffrey Harris jeffrey at osafoundation.org
Mon Jul 2 12:51:07 CDT 2007


Hello Archatas,

> Could you please, write an example of code how to create vCard and
> iCalendar objects with text in UTF-8?
> 
> When I simply assign UTF-8-based street addresses or other info to the
> properties of vCard, I usually get errors like these:
> 'ascii' codec can't decode byte 0xc3 in position 11: ordinal not in
> range(128)

The Address helper class has a __repr__ method that doesn't escape
unicode (that's a bug), so if you do this, you'll get a traceback:

>>> import vobject
>>> a = vobject.vcard.Address(u'R\u0120ndom', 'berkeley', 'ca',
...                           '94704', 'USA')
>>> print a
Traceback (most recent call last):
...
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0120' in
position 1: ordinal not in range(128)

However, you can do something like this:

>>> unicode(a)
u'R\u0120ndom\nberkeley, ca 94704\nUSA'

And you can assign this address to a vCard:

>>> card = vobject.vCard()
>>> card.add('adr').value = a
>>> card.add('fn').value  = 'Jeffrey Harris'
>>> card.add('n').value   = vobject.vcard.Name(family='Harris',
...                                            given='Jeffrey')

The vCard should serialize just fine, the output will be unicode.

>>> card.serialize()
u'BEGIN:VCARD\r\nVERSION:3.0\r\nADR:;;R\u0120ndom;berkeley;ca;94704;USA
\r\nFN:Jeffrey Harris\r\nN:Harris;Jeffrey;;;\r\nEND:VCARD\r\n'

You can't just print unicode with non-ASCII code points, however,

>>> print card.serialize()
Traceback (most recent call last):
...
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0120' in
position 33: ordinal not in range(128)

In Python, if you want to encode unicode as UTF-8, you have to use the
encode method:

>>> print card.serialize().encode('utf-8')
BEGIN:VCARD
VERSION:3.0
ADR:;;RÄndom;berkeley;ca;94704;USA
FN:Jeffrey Harris
N:Harris;Jeffrey;;;
END:VCARD

Sincerely,
Jeffrey


More information about the VObject mailing list