使用vobject在django-python中解析vcf文件

时间:2016-07-23 14:31:11

标签: python django vcf vobject

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

text-align: center;
解析时我得到了以下错误:

  

/ importcontactsfromvcard /

中的ParseError      

第12行:无法解析行:纽约,纽约928293

我正在使用以下代码:

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

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

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

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

1 个答案:

答案 0 :(得分:1)

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

以下是规范:RFC5545 Section 3.1