Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

String split in python for ldap return values

My LDAP Returns values as below

[('CN=b12345,OU=Accounts,DC=mydomain,DC=com', {'Fname': [Jose, Movi'], 'location': [b'London'], 'mail': [b'jose.movi@google.com'], 'title': [b'Sales -Exec -Retail']})]
[('CN=h12345,OU=Accounts,DC=mydomain,DC=com', {'Fname': [Lorraine, Moses'], 'location': [b'Boston'], 'mail': [b'Lorraine.Moses@google.com'], 'title': [b'Sales -ExecIII -Retail']})]
[('CN=o12345,OU=Accounts,DC=mydomain,DC=com', {'Fname': [Andy, Cameron'], 'location': [b'NewYork'], 'mail': [b'Andy.Cameron@google.com'], 'title': [b'Customer Support II - Fixed']})]

I would like to split them as below for each entry and print them as below

DisplayName:Jose, Movi
Location:London
Email:jose.movi@google.com
Designation:Sales -Exec -Retail

Apologize to me in advance as I’m a beginner in python.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Individual lists

When you define x as:

x = [('CN=b12345,OU=Accounts,DC=mydomain,DC=com',
      {'Fname': ['Jose, Movi'],
       'location': [b'London'],
       'mail': [b'jose.movi@google.com'],
       'title': [b'Sales -Exec -Retail']})]

You can print in a key value manner:

for k, v in x[0][1].items():
    try:
        print(f"{k.title()}: {', '.join(a.decode('utf-8') for a in v)}")
    except (UnicodeEncodeError, AttributeError):
        # If you try to decode a regular string
        print(f"{k.title()}: {', '.join(v)}")

Outputs

Fname: Jose, Movi
Location: London
Mail: jose.movi@google.com
Title: Sales -Exec -Retail

Or assign custom values for each key:

def printer(k, v):
    try:
        print(f"{k.title()}: {', '.join(a.decode('utf-8') for a in v)}")
    except (UnicodeEncodeError, AttributeError):
        # If you try to decode a regular string
        print(f"{k.title()}: {', '.join(v)}")


for k, v in x[0][1].items():
    if k == "Fname":
        printer("DisplayName", v)
    elif k == "mail":
        printer("Email", v)
    elif k == "title":
        printer("Designation", v)
    else:
        printer(k, v)

Outputs:

Displayname: Jose, Movi
Location: London
Email: jose.movi@google.com
Designation: Sales -Exec -Retail

If you are using >= Python3.10 you can use match statements for the custom titles:

for k, v in x[0][1].items():
    match k:
        case "Fname":
            printer("DisplayName", v)
        case "mail":
            printer("Email", v)
        case "title":
            printer("Designation", v)
        case _:
            # Default case
            printer(k, v)

All lists

When you define x as:

x = [[('CN=b12345,OU=Accounts,DC=mydomain,DC=com',
       {'Fname': ['Jose, Movi'],
        'location': [b'London'],
        'mail': [b'jose.movi@google.com'],
        'title': [b'Sales -Exec -Retail']})],
     [('CN=h12345,OU=Accounts,DC=mydomain,DC=com',
      {'Fname': ['Lorraine, Moses'],
       'location': [b'Boston'],
       'mail': [b'Lorraine.Moses@google.com'],
       'title': [b'Sales -ExecIII -Retail']})],
     [('CN=o12345,OU=Accounts,DC=mydomain,DC=com',
      {'Fname': ['Andy, Cameron'],
       'location': [b'NewYork'],
       'mail': [b'Andy.Cameron@google.com'],
       'title': [b'Customer Support II - Fixed']})]]

You can edit your examples to be in another for loop for i:

for i in x:
    for k, v in i[0][1].items():
    # Rest of method goes here

If you are strictly working with single element lists, you can change ', '.join(a.decode('utf-8') for a in v) to v.decode('utf-8'). And ', '.join(v) to v

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading