[Vobject] importing UTF-8 text - UnicodeDecodeError
Matthew Leingang
leingang at courant.nyu.edu
Mon Mar 15 20:24:12 CDT 2010
Hello,
I'm using vobject to parse data from a couple of CSV files and merge them to vcards, eventually to import into Mac Address Book. One of the files is ASCII, the other UTF-8. The UTF-8 file is survey data that I'm just munging and putting into the Note field.
Upon serialization, I get the following error and traceback:
File "./studinfo2vcard.py", line 194, in <module>
card_str=card.serialize()
File "./vobject-0.8.1c/vobject/base.py", line 186, in serialize
return behavior.serialize(self, buf, lineLength, validate)
File "./vobject-0.8.1c/vobject/behavior.py", line 157, in serialize
out = base.defaultSerialize(transformed, buf, lineLength)
File "./vobject-0.8.1c/vobject/base.py", line 945, in defaultSerialize
child.serialize(outbuf, lineLength, validate=False)
File "./vobject-0.8.1c/vobject/base.py", line 186, in serialize
return behavior.serialize(self, buf, lineLength, validate)
File "./vobject-0.8.1c/vobject/behavior.py", line 157, in serialize
out = base.defaultSerialize(transformed, buf, lineLength)
File "./vobject-0.8.1c/vobject/base.py", line 958, in defaultSerialize
s.write(':' + obj.value)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/codecs.py", line 351, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 91: ordinal not in range(128)
I think I understand the problem: vcard is an ASCII-only specification, and the serialize method can't encode the unicode text as ASCII. It's hard to say what in the file causes this error; because if I try to do individual records it will sometimes work, and single records on which it barfs don't seem to have anything special about them.
Is there a way to programmatically write non-ASCII text to a vCard? I know that I can put non-ASCII characters into the Note field within Address Book. Trouble is, when I export the vCard, the Note field isn't exported. So I don't have a model for coaxing Address Book to do what I want.
I tried quoted-printable for the encoding, but Address Book didn't decode it (left it as quoted-printable gobbledygook). Then I tried binary, and Address Book crashed on import.
I could just try iconv to strip out the non-ASCII characters but that seems like a waste. Any clues would be appreciated.
Best,
Matthew
--
Matthew Leingang
Clinical Associate Professor of Mathematics
leingang at courant.nyu.edu
More information about the VObject
mailing list