2017-02-01 11 views
0

을 사용하여 vCard를의 속성에 대한 모든 값을 가져옵니다 :파이썬 vobject

with open(some_file_containing_a_vcard, 
        "r" , encoding="utf-8") as fd: 
      vcard_content = vobject.readOne(fd.read(), 
              ignoreUnreadable=True) 

어떻게에 의해 제공보다 더이 있다면 나는 모든 전화 번호 또는 이메일을 얻을 fo를 vcard?

vcard_content.tel.value 
vcard_content.email.value 

을 ... 그러나 이것은 각의 첫 번째 반환

는 I은 발견했다.

코드로 나뉘어 진 것처럼 엔티티가 클론으로 생성 된 것처럼 보입니다. 따라서 vcard의 속성 "TEL"는 "tel"로 생성됩니다. 셀과 직장 전화 번호가 있다면?

나는 완전히 붙어 :-)에게 생각 나는 다음과 같은 코드로 해결

+0

함께 작업하는 vCard도 제공 할 수 있습니까? – wpercy

+0

불행히도 아닙니다. 하지만 그 동안 문제를 해결했고 나중에이 질문을 업데이트 할 것입니다. 나는 getChildren()을 사용하고 vcard의 모든 라인을 반복하여 해결했다. 이렇게하면 모든 정보를 가져옵니다. 예는 다음과 같습니다. –

+0

위대한, 당신이 그것을 알아낼 수 있었다니 다행 – wpercy

답변

0

:

stream = io.open(some_file_containing_a_vcard, "r", encoding="utf-8") 
vcf = vobject.readComponents(stream, ignoreUnreadable=True) 

for child in vcard.getChildren(): 
    name = child.name.lower() 
    value = child.value 
    attribs = child.params.get("TYPE", []) 

    filtered_type = [x.lower() for x in attribs if x.lower() in (
         "work", "home", "cell", "fax" 
        )] 
    if len(filtered_type) == 0: 
     filtered_type = ["unspec"] 

    # Do something with it... i.e. adding to a dict() 

stream.close() 

0

:-) 나를 위해 작동 당신은 실제로를 사용하여이를 달성 할 수 contents 속성은 사전입니다. 그래서 당신은 (하나 또는 없음이 경우에도) 모든 TEL 속성의 목록을 인쇄 할

with open(path_to_vcard) as f: 
    text = f.read() 

card = vobject.readOne(text) 
print card.contents['tel'] 

같은 것을 사용하여 모든 TEL 속성 목록에 액세스합니다.