Here is content inside .vcf file data.
BEGIN:VCARD
VERSION:4.0
N:Muller;CCCIsabella;;;
FN:Muller
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
PHOTO;MEDIATYPE=image/gif:http://www.example.com/dir_photos/my_photo.gif
TEL;TYPE=work,voice;VALUE=uri:tel:+16829185770
REV:20080424T195243Z
END:VCARD
BEGIN:VCARD
VERSION:4.0
N:Mraz;CCCEdwardo;;;
FN:Mraz
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
PHOTO;MEDIATYPE=image/gif:http://www.example.com/dir_photos/my_photo.gif
TEL;TYPE=work,voice;VALUE=uri:tel:+18083155095
REV:20080424T195243Z
END:VCARD
BEGIN:VCARD
VERSION:4.0
N:Reynolds;CCCBrant;;;
FN:Reynolds
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
PHOTO;MEDIATYPE=image/gif:http://www.example.com/dir_photos/my_photo.gif
TEL;TYPE=work,voice;VALUE=uri:tel:+15089473508
REV:20080424T195243Z
END:VCARD
I want my data in as below.
data = [{'name': 'Muller','phone': '+16829185770'}, {'name': 'Mraz', 'phone': '+18083155095'}, {'name': 'Reynolds','phone': '+15089473508'}]
but I am not getting data as above. Please help me out in this case. Here I am using re python package to solve.
import re
file = open('contacts.vcf', 'r')
contacts = []
for line in file:
name = re.findall('FN:(.*)', line)
tel = re.findall('tel:(.*)', line)
nm = ''.join(name)
tel = ''.join(tel)
if len(nm) == 0 and len(tel) == 0:
continue
data = {'name' : nm, 'phone' : tel}
contacts.append(data)
print(contacts)
getting below results name and phone are adding in defferent.
[{'name': 'Muller', 'phone': ''}, {'name': '', 'phone': '+16829185770'}, {'name': 'Mraz', 'phone': ''}, {'name': '', 'phone': '+18083155095'}, {'name': 'Reynolds', 'phone': ''}, {'name': '', 'phone': '+15089473508'}]
You can try the below code.
import re
file = open('vcards-2.vcf', 'r')
contacts = []
phone = []
for line in file:
name = re.findall('FN:(.*)', line)
nm = ''.join(name)
if len(nm) == 0:
continue
data = {'name' : nm.strip()}
for lin in file:
tel = re.findall('pref:(.*)', lin)
tel = ''.join(tel)
if len(tel) == 0:
continue
tel = tel.strip()
tel = ''.join(e for e in tel if e.isalnum())
data['phone'] = tel
break
contacts.append(data)
print(contacts)
You will get below redults
[{'name': 'Muller','phone': '+16829185770'}, {'name': 'Mraz', 'phone': '+18083155095'}, {'name': 'Reynolds','phone': '+15089473508'}]