如何将VCard解析为Python字典? [英] How do I parse a VCard to a Python dictionary?

查看:133
本文介绍了如何将VCard解析为Python字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何使用 VObject 将VCard解析为Python字典.

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

这就是我所得到的:

{'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)}

如何用我的VCard数据填充字典?

How can I populate the dictionary with my VCard data?

推荐答案

您不想查看行为,而是想要查看vobj本身.该行为是一个数据结构,描述了需要/期望哪些子级,以及如何将这些子级转换为适当的Python数据结构.

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.

vobj对象是vobject组件.它的contents属性是vobject ContentLines和可能的Components的字典,所以

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

vobject.contents

将为您提供对象字典.

如果您想对解析的内容有更人性化的了解,请执行以下操作:

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

vobj.prettyPrint()

例如,要访问单个孩子,请执行以下操作:

To access individual children, do, for instance:

vobj.adr

这篇关于如何将VCard解析为Python字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆