1

我正在使用 vobject 模块来解析从 MS Outlook 导出的 vcard。它的内容如下:

BEGIN:VCARD
VERSION:2.1
N:Salei;Roya
FN:Roya Salei
ORG:MealPlanBook
TEL;WORK;VOICE:(3) 228-7373
TEL;HOME;VOICE:2343243423
TEL;CELL;VOICE:3243443234
TEL;PREF:310-889-8166
ADR;WORK;PREF:;;street abc;new york;new york;928293;United States of America
LABEL;WORK;PREF;ENCODING=QUOTED-PRINTABLE:street abc=0D=0A=
new york, new york  928293
X-MS-OL-DEFAULT-POSTAL-ADDRESS:2
URL;WORK:www.roya.com
EMAIL;PREF;INTERNET:roya@gmail.com
X-MS-CARDPICTURE;TYPE=JPEG;ENCODING=BASE64:
 /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAcFBQYFBAcGBQYIBwcIChELCgkJChUPEAwRGBUa
 GRgVGBcbHichGx0lHRcYIi4iJSgpKywrGiAvMy8qMicqKyr/2wBDAQcICAoJChQLCxQqHBgc
 KioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKir/wAAR
 CACUACcDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAA
 AgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkK
 FhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWG
 h4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl
 5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREA
 AgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYk
 NOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOE
 hYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk
 5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD393C4B6mgHqSeKhc7nOPoKRj2HQUAPef+
 5z7mlj5GT19agAyasD5U59M0AQyfPNj0opIhliaKAHDjJ9BSUp+7SUAEYy1SzHEZ9+KbCOaS
 Y8gUANU7Yie+aKJOFVaKAFbrxSUdqB1oAnjHy5qA/NN+NWD8kf0FV4uWJ9qAElOXxRTWbJJ9
 6KAH9qdGPmFN7VLEP5YoAJmwgHrUa/LGT60sxy+PQUEEqFXk96AIKKmEar9880UAA5OKlQ4X
 jknmkDIei8/SlclUOOKAG+WN2X5J7CmvIRwo28UkfLEnrimuQXNADVG5vmNFJjJwKKALEY+Y
 fnSzHoPxpY1+U/lTHZdx4yenPQUAEYIXPvUeFXqcn0FSOf3fPHAHFRUABc9F+X6UUyigC8eE
 57VXA3MPc1PKcRn3qFOpPoKACQ9PxNR06T72PTim0AMooooAszH5gPSkj+7z3NI5yx+tOPCf
 QfzoAhJyc0UHrRQAyiiigCyH3EAilYrjnuaYnUn0FI/UD0FABsU9P0NIY/f8xTT1oDEdCRQA
 bG7c/Q0Ubz3AP4UUASp91vwprffb60UUAMPWiiigBlFFFAH/2Q==

X-MS-OL-DESIGN;CHARSET=utf-8:<card xmlns="http://schemas.microsoft.com/office/outlook/12/electronicbusinesscards" ver="1.0" layout="left" bgcolor="ffffff"><img xmlns="" align="fit" area="16" use="cardpicture"/><fld xmlns="" prop="name" align="left" dir="ltr" style="b" color="000000" size="10"/><fld xmlns="" prop="org" align="left" dir="ltr" color="000000" size="8"/><fld xmlns="" prop="telwork" align="left" dir="ltr" color="000000" size="8"><label align="right" color="626262">Work</label></fld><fld xmlns="" prop="telpri" align="left" dir="ltr" color="000000" size="8"><label align="right" color="626262">Primary</label></fld><fld xmlns="" prop="telcell" align="left" dir="ltr" color="000000" size="8"><label align="right" color="626262">Mobile</label></fld><fld xmlns="" prop="telhome" align="left" dir="ltr" color="000000" size="8"><label align="right" color="626262">Home</label></fld><fld xmlns="" prop="email" align="left" dir="ltr" color="000000" size="8"/><fld xmlns="" prop="addrwork" align="left" dir="ltr" color="000000" size="8"/><fld xmlns="" prop="webwork" align="left" dir="ltr" color="000000" size="8"/><fld xmlns="" prop="blank" size="8"/><fld xmlns="" prop="blank" size="8"/><fld xmlns="" prop="blank" size="8"/><fld xmlns="" prop="blank" size="8"/><fld xmlns="" prop="blank" size="8"/><fld xmlns="" prop="blank" size="8"/><fld xmlns="" prop="blank" size="8"/></card>
REV:20160723T142140Z
END:VCARD

解析时出现以下错误:

/importcontactsfromvcard/ 处的 ParseError

第 12 行:解析行失败:new york, new york 928293

我正在使用以下代码:

in_data = request.body
vcard = vobject.readOne(in_data)
contents = vcard.contents

当我从文件中省略一些内容时,它们工作得非常好,它看起来如下:

BEGIN:VCARD
VERSION:2.1
N:Salei;Roya
FN:Roya Salei
ORG:MealPlanBook
TEL;WORK;VOICE:(3) 228-7373
TEL;HOME;VOICE:2343243423
TEL;CELL;VOICE:3243443234
TEL;PREF:310-889-8166
ADR;WORK;PREF:;;street abc;new york;new york;928293;United States of America
END:VCARD

我是 python 新手,第一次使用 vcards。我不知道如何忽略上述错误来解析 Vcard。

4

1 回答 1

1

发生这种情况是因为您没有正确折叠线条。有问题的行,第 12 行,包含new york, new york 928293,它实际上是它上面的行的延续。为了使 vObject 能够读取它,必须通过在其前面加上一个空格来“折叠”它,以让解析器知道它实际上是上面一行的一部分。

这是规范:RFC5545 第 3.1 节

于 2017-06-20T17:38:19.387 回答