[Vobject] unicode in vcards

Jeffrey Harris jeffrey at osafoundation.org
Wed Dec 12 20:04:04 CST 2007


Hi Anil,

> f.write(serialize)
> 
> This last line throws this exception:
> <type 'exceptions.UnicodeEncodeError'>: 'ascii' codec can't encode
> characters in position 29-31: ordinal not in range(128)
> 
> I think j.serialize() returns the data in unicode, but how come this
> exception is being thrown when saving to that file?

If you want to write unicode to a file, you need to encode the unicode.
 There are various ways to do this, the easiest, I think, is:

import codecs

f = codecs.open('test.txt', 'w', 'utf-8')
f.write(u'\u00bfHabla espa\u00f1ol?')
f.close()

Sincerely,
Jeffrey


More information about the VObject mailing list