Vcard parser with Python

Viewed 16620

I am parsing my vcard info (copied to a txt file)to extract name:number and put it into a dictionary.

Data sample:

BEGIN:VCARD  
VERSION:2.1  
N:MEO;Apoio;;;  
FN:Apoio MEO  
TEL;CELL;PREF:1696  
TEL;CELL:162 00  
END:VCARD  
BEGIN:VCARD  
VERSION:2.1  
N:estrangeiro;Apoio MEO;no;;  
FN:Apoio MEO no estrangeiro  
TEL;CELL;PREF:+35196169000  
END:VCARD  
import re
file = open('Contacts.txt', 'r')
contacts = dict()

    for line in file:
            name = re.findall('FN:(.*)', line)
            nm = ''.join(name)

            if len(nm) == 0:
                continue
            contacts[nm] = contacts.get(nm)
    print(contacts)

With this I am getting a dictionary with names but for numbers I am getting None. {'name': None, 'name': None}.

Can I do this with re? To extract both name and number with the same re.findall expression?

2 Answers
Related