简体   繁体   中英

transform Python dictionary with bytes to string in python 3.8

I am hoping people can help me. I apologize in advance for any grammatical mistakes. I am working on large scale porting of LDAP data to MYSQL database. The LDAP dump from python program I "inherited". For performance and maintenance reasons, we are moving the data out of LDAP and into web authentication system that uses MySQL as the backend. For business reasons, the LDAP to webauth migration is going to happen over the next year -- so I need to keep the two systems in sync.

Having used python 2 for years --- Unicode / UTF-8 in python 3 is an area that causes me much pain.

The ldap dump produces the following output as part of the execution

{'sn': [b'Jones'], 'title': [b'WH Trainee'], 'givenName': [b'Example'], 'distinguishedName': [b'CN=Example Jones,OU=Warehouse Trainees,OU=GU,DC=jupiter,DC=somecorp,DC=org'], 'whenCreated': [b'20201027121144.0Z'], 'department': [b'WHSE'], 'sAMAccountName': [b'ejones'], 'manager': [b'CN=Bobby Smith,OU=WHSE,OU=GU,DC=jupiter,DC=somecorp,DC=org']}

Formatted for easier reading: ** existing output**

{
'sn': [b'Jones'], 
'title': [b'WH Trainee'], 
'givenName': [b'Example'], 
'distinguishedName': [b'CN=Example Jones,OU=Warehouse Trainees,OU=GU,DC=jupiter,DC=somecorp,DC=org'], 
'whenCreated': [b'20061027132244.0Z'], 
'department': [b'WHSE'], 
'sAMAccountName': [b'ejones'], 
'manager': [b'CN=Bobby Smith,OU=WHSE,OU=GU,DC=jupiter,DC=somecorp,DC=org']
}

We need to convert this output to simple string, key value pairs in a dictionary. I would like to have this implemented as a simple function that can be called aond convert all of the values in a dictionary at once. Unfortunately some ldap records are missing things like departments and managers, so the number of keys and values in the dict varies. I need a way to transform the byte values in the dict to plain ascii strings.

Desired transformation using single function call something like transformdict(dictname)

desired output

{
'sn': 'Jones', 
'title': 'WH Trainee', 
'givenName': 'Example', 
'distinguishedName': 'CN=Example Jones,OU=Warehouse Trainees,OU=GU,DC=jupiter,DC=somecorp,DC=org', 
'whenCreated': '20061027132244.0Z', 
'department': 'WHSE', 
'sAMAccountName': 'ejones', 
'manager': 'CN=Bobby Smith,OU=WHSE,OU=GU,DC=jupiter,DC=somecorp,DC=org'
}

We using python 3.8 environment.

Any suggestions?

This should do it:

S = {'sn': [b'Jones'], 'title': [b'WH Trainee'], 'givenName': [b'Example'], 'distinguishedName': [b'CN=Example Jones,OU=Warehouse Trainees,OU=GU,DC=jupiter,DC=somecorp,DC=org'], 'whenCreated': [b'20201027121144.0Z'], 'department': [b'WHSE'], 'sAMAccountName': [b'ejones'], 'manager': [b'CN=Bobby Smith,OU=WHSE,OU=GU,DC=jupiter,DC=somecorp,DC=org']}

dt = {key: value[0].decode("utf-8") for key, value in S.items()}
print(dt)

Output:

{
    "sn": "Jones",
    "title": "WH Trainee",
    "givenName": "Example",
    "distinguishedName": "CN=Example Jones,OU=Warehouse Trainees,OU=GU,DC=jupiter,DC=somecorp,DC=org",
    "whenCreated": "20201027121144.0Z",
    "department": "WHSE",
    "sAMAccountName": "ejones",
    "manager": "CN=Bobby Smith,OU=WHSE,OU=GU,DC=jupiter,DC=somecorp,DC=org"
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM