How do I parse a VCard to a Python dictionary?

ε祈祈猫儿з 提交于 2019-12-04 13:19:14

问题


I'm trying to figure out how to parse a VCard to a Python dictionary using VObject.

vobj=vobject.readOne(string)
print vobj.behavior.knownChildren

This is all I get:

{'CATEGORIES': (0, None, None), 'ADR': (0, None, None), 'UID': (0, None, None), 'PHOTO': (0, None, None), 'LABEL': (0, None, None), 'VERSION': (1, 1, None), 'FN': (1, 1, None), 'ORG': (0, None, None), 'N': (1, 1, None), 'PRODID': (0, 1, None)}

How can I populate the dictionary with my VCard data?


回答1:


You don't want to look at the behavior, you want to look at vobj itself. The behavior is a data structure describing what children are required/expected, and how to translate those children into appropriate Python data structures.

The vobj object is a vobject Component. Its contents attribute is a dictionary of vobject ContentLines and possibly Components, so

vobject.contents

will give you a dictionary of objects.

If you want a more human readable view of what was parsed, do:

vobj.prettyPrint()

To access individual children, do, for instance:

vobj.adr


来源:https://stackoverflow.com/questions/2478027/how-do-i-parse-a-vcard-to-a-python-dictionary

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!