Python LDAP3 -如何将结果条目获取到Pandas数据框架中



我试图从Python3(3.9)模块LDAP3获得结果到Pandas DataFrame,因此我可以更好地操作内容。

我可以执行一个简单的:

for entry in conn.entries:
print(str(entry.enter_raw_attributes))

以下是打印结果示例:

{'sAMAccountName': [b'username1'], 'mail': [b'Jane.Doe@organisation.org']}
{'sAMAccountName': [b'username2'], 'mail': [b'Frank.Doe@organisation.org']}
{'sAMAccountName': [b'username3'], 'mail': [b'Mark.Doe@organisation.org']}
{'sAMAccountName': [b'username4'], 'mail': []}

我希望在Pandas Dataframe中看到如下内容:

{
"sAMAccountName": [username1, username2, username3, username4],
"mail": [Jane.Doe@organisation.org, Frank.Doe@organisation.org, Mark.Doe@organisation.org, NaN]
}
sAMAccountName  mail
0       username1        Jane.Doe@organisatiin.org
1       username2        Frank.Doe@organisation.org
2       username3        Mark.Doe@organisation.org
3.      username4
  • 我努力添加字典和各种列表,但都失败了。

  • 我看到冒犯的'b'弹出在前面的值也需要被删除(例如b'username1'或b'Mark.Doe@organisation.org')。

  • 我不确定如何在DataFrame

    中为丢失的电子邮件设置NaN (null)值

如果能帮助我正确填充Pandas DataFrame,我将不胜感激。

b'表示它是一个字节串,是请求enter_raw_attributes的结果。

您可能想要直接调用您正在查找的属性,或者将其转换为可以消化的另一种数据类型。

文档中有一个处理返回数据的不同方法的示例:

>>> # get the DN of an entry
>>> r[0].entry_dn
'cn=b.young,ou=ldap3-tutorial,dc=demo1,dc=freeipa,dc=org'
>>> # query the attributes in the Entry as a list of names
>>> r[0].entry_attributes
['destinationIndicator', 'x500UniqueIdentifier', 'audio', 'photo', 'uid', 'l', 'pager', 'carLicense', 'street', 'teletexTerminalIdentifier', 'o', 'st', 'homePostalAddress', 'preferredDeliveryMethod', 'roomNumber', 'sn', 'homePhone', 'x121Address', 'displayName', 'userSMIMECertificate', 'userPassword', 'title', 'physicalDeliveryOfficeName', 'mail', 'initials', 'ou', 'businessCategory', 'seeAlso', 'jpegPhoto', 'registeredAddress', 'facsimileTelephoneNumber', 'postalAddress', 'telephoneNumber', 'mobile', 'labeledURI', 'postalCode', 'objectClass', 'employeeNumber', 'secretary', 'employeeType', 'description', 'cn', 'userCertificate', 'userPKCS12', 'postOfficeBox', 'departmentNumber', 'givenName', 'internationalISDNNumber', 'preferredLanguage', 'telexNumber', 'manager']
>>> # query the attributes in the Entry as a dict of key/value pairs
>>> r[0].entry_attributes_as_dict
{'destinationIndicator': [], 'x500UniqueIdentifier': [], 'audio': [], 'photo': [], 'uid': [], 'l': [], 'pager': [], 'carLicense': [], 'street': [], 'teletexTerminalIdentifier': [], 'o': [], 'homePostalAddress': [], 'preferredDeliveryMethod': [], 'roomNumber': [], 'st': [], 'homePhone': [], 'x121Address': [], 'displayName': [], 'userSMIMECertificate': [], 'userPassword': [], 'title': [], 'physicalDeliveryOfficeName': [], 'mail': [], 'preferredLanguage': [], 'initials': [], 'internationalISDNNumber': [], 'ou': [], 'businessCategory': [], 'seeAlso': [], 'jpegPhoto': [], 'registeredAddress': [], 'facsimileTelephoneNumber': [], 'postalAddress': [], 'telephoneNumber': ['1111'], 'mobile': [], 'labeledURI': [], 'postalCode': [], 'objectClass': ['inetOrgPerson', 'organizationalPerson', 'person', 'top'], 'employeeNumber': [], 'description': [], 'employeeType': [], 'secretary': [], 'cn': ['b.young'], 'userPKCS12': [], 'postOfficeBox': [], 'departmentNumber': ['DEV'], 'givenName': ['Beatrix'], 'sn': ['Young'], 'userCertificate': [], 'telexNumber': [], 'manager': []}

>>> # let's check which attributes are mandatory
>>> r[0].entry_mandatory_attributes
['sn', 'objectClass', 'cn']
>>> # convert the Entry to LDIF
>>> print(r[0].entry_to_ldif())
version: 1
dn: cn=b.young,ou=ldap3-tutorial,dc=demo1,dc=freeipa,dc=org
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
objectClass: top
sn: Young
telephoneNumber: 1111
cn: b.young
departmentNumber: DEV
givenName: Beatrix
# total number of entries: 1
>>> print(r[0].entry_to_json(include_empty=False))  # Use include_empty=True to include empty attributes
{
"attributes": {
"cn": [
"b.young"
],
"departmentNumber": [
"DEV"
],
"givenName": [
"Beatrix"
],
"objectClass": [
"inetOrgPerson",
"organizationalPerson",
"person",
"top"
],
"sn": [
"Young"
],
"telephoneNumber": [
"1111"
]
},
"dn": "cn=b.young,ou=ldap3-tutorial,dc=demo1,dc=freeipa,dc=org"
}

相关内容

  • 没有找到相关文章

最新更新