Python Forum
Convert email addresses to VCF format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert email addresses to VCF format
#2
It was easier to look at doing this with the LDIF format. Here is the code:

#!/usr/bin/env python

from ldif3 import LDIFParser
from pprint import pprint

parser = LDIFParser(open("claws_export.ldif", "rb"))

for dn, record in parser.parse():
    
    name = ""
    if 'cn' in record:
        name = record['cn'][0]
    
    surname = ""
    if 'sn' in record:
        surname = record['sn'][0]
    
    given_name = ""
    if 'givenName' in record:
        given_name = record['givenName'][0]
    
    display_name = ""
    if 'displayName' in record:
        display_name = record['displayName'][0]
    
    email = ""
    if 'mail' in record:
        email = record['mail'][0]

    print ('BEGIN:VCARD')
    print ('VERSION:2.1')
    print ("N:" + surname + ";" + given_name + ";;;")
    print ("FN:" + name)
    print ("EMAIL;HOME:" + email)
    print ("END:VCARD")
The o/p data looks like it is nearly matching what is required for import into the Galaxy contacts. If I did a
print(record)
this was the output

Quote:OrderedDict([('objectClass', ['inetOrgPerson']), ('cn', ['Forrest Gump']), ('sn', ['Gump']), ('displayName', ['Forrest Gump']), ('mail', ['[email protected]'])])

Can any improvements be done to the code ? For example, it seems a waste having to do all those "if" statements; possibly python has some sort of lookup function, to lookup within the class ?
Reply


Messages In This Thread
Convert email addresses to VCF format - by jehoshua - Mar-04-2021, 10:22 PM
RE: Convert email addresses to VCF format - by jehoshua - Mar-05-2021, 08:33 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 768 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Convert Json to table format python_student 2 5,582 Sep-28-2022, 12:48 PM
Last Post: python_student
  Convert .xlsx to Format as Table bnadir55 0 898 Aug-11-2022, 06:39 AM
Last Post: bnadir55
  a function to get IP addresses of interfaces Skaperen 2 1,443 May-30-2022, 05:00 PM
Last Post: Skaperen
  how to convert and format a varable darktitan 4 1,707 May-29-2022, 12:59 PM
Last Post: darktitan
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 4,007 May-11-2022, 02:23 PM
Last Post: menator01
  Convert Date to another format lonesoac0 2 1,681 Mar-17-2022, 11:26 AM
Last Post: DeaD_EyE
  Convert timedelta to specified format Planetary_Assault_Systems 3 3,217 Jun-27-2021, 01:46 PM
Last Post: snippsat
  How to convert dates in odd format to months lokhtar 2 2,253 Apr-17-2021, 11:54 AM
Last Post: lokhtar
  instance methods sharing addresses mim 1 2,249 Mar-28-2021, 05:22 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020