3

vobject在python中使用。我正在尝试解析vcard位于此处的:

http://www.mayerbrown.com/people/vCard.aspx?Attorney=1150

为此,我执行以下操作:

    import urllib
    import vobject

    vcard = urllib.urlopen("http://www.mayerbrown.com/people/vCard.aspx?Attorney=1150").read()
    vcard_object = vobject.readOne(vcard)

每当我这样做时,我都会收到以下错误:

Traceback (most recent call last):
  File "<pyshell#86>", line 1, in <module>
    vobject.readOne(urllib.urlopen("http://www.mayerbrown.com/people/vCard.aspx?Attorney=1150").read())
  File "C:\Python27\lib\site-packages\vobject-0.8.1c-py2.7.egg\vobject\base.py", line 1078, in readOne
    ignoreUnreadable, allowQP).next()
  File "C:\Python27\lib\site-packages\vobject-0.8.1c-py2.7.egg\vobject\base.py", line 1031, in readComponents
    vline = textLineToContentLine(line, n)
  File "C:\Python27\lib\site-packages\vobject-0.8.1c-py2.7.egg\vobject\base.py", line 888, in textLineToContentLine
    return ContentLine(*parseLine(text, n), **{'encoded':True, 'lineNumber' : n})
  File "C:\Python27\lib\site-packages\vobject-0.8.1c-py2.7.egg\vobject\base.py", line 262, in __init__
    self.value = str(self.value).decode('quoted-printable')
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 29: ordinal not in range(128)

我已经尝试了许多其他变体,例如转换vcardunicode,使用各种编码等。但我总是收到相同或非常相似的错误消息。

有想法该怎么解决这个吗?

4

3 回答 3

2

它在 vCard 的第 13 行失败,因为该ADR属性被错误地标记为以“引用打印”编码进行编码。该ü字符应编码为=FC,这就是vobject引发错误的原因。

于 2012-12-28T14:10:49.647 回答
0

vobject图书馆readOne方法很尴尬。

为了避免出现问题,我决定将 vcards 以引用的可打印数据的形式保存在我的数据库中,这是人们喜欢的。

假设some_vcard是 UTF-8 编码的字符串

quopried_vcard = quopri.encodestring(some_vcard)

并且quopried_vcard得到持久化,并且在需要时:

vobj = vobject.readOne(quopried_vcard)

然后取回解码数据,例如fnvcard 中的字段:

quopri.decodestring(vobj.fn.value)

也许有人可以用 readOne 更好地处理 UTF-8。如果是的话,我很乐意看到它。

于 2015-04-09T15:48:52.403 回答
0

文件以 UTF-8(我认为)编码字符串的形式下载,但库试图将其解释为 ASCII。

尝试在 urlopen 之后添加以下行:

vcard = vcard.decode('utf-8')
于 2012-12-28T10:22:16.577 回答