简体   繁体   中英

Search for key word and convert the output into new line after it matches in python

I have below List output from the a code which i'm working in python where i'm specifically looking for memberUid string and want every names after that to be printed into new line: like: anshulm jiafan

and while prnting these names as soon it will get 'cn' just stop the print.

[[('cn=oracle,ou=Group,ou=corp,ou=services,o=kk.com', {'description': ['oracle group'], 'businessCategory': ['Private'], 'objectClass': ['top', 'groupOfUniqueNames', 'posixGroup'], 'memberUid': ['anshulm', 'jiafan', 'manasij', 'asbisht', 'karnika', 'junle', 'amitsh', 'fuwei', 'dewansh', 'gouravr', 'harshitb', 'tandel', 'matte', 'izamir', 'elie', 'emiliano', 'mateuszw', 'theo', 'mahdi', 'hassan', 'gshruti', 'makhiles', 'prabhaka', 'shgarg', 'ritolia', 'wadhwani', 'steev', 'rtlsbld', 'nikhilb', 'fwang', 'ankitb', 'rtls', 'amitb', 'agautam', 'pratyush', 'hywang', 'dsouder', 'foutz', 'parimi', 'pradeepn', 'patrickg', 'pkunwar', 'tejinder', 'ramteke', 'jangra', 'kush', 'kundan', 'mohang', 'xiang', 'xinjia', 'anantv', 'christos', 'achugh', 'kbhatt', 'jroy', 'kusantos', 'kamleshm', 'iraa', 'indrajit'], 'gidNumber': ['9393'], 'owner': ['varshney'], 'cn': ['oracle']})]]

Below is my code which is yielding the above output:

import ldap

## first you must open a connection to the server
try:
        l = ldap.initialize("ldap://ldapserver:389")
        l.protocol_version = ldap.VERSION3
except ldap.LDAPError, e:
        print e

    baseDN = "ou=group,ou=corp,ou=services,o=kk.com"
    searchScope = ldap.SCOPE_SUBTREE
    retrieveAttributes = None
    searchFilter = raw_input("Enter the Group Name: ")
    try:
            ldap_result_id = l.search(baseDN, searchScope, searchFilter, retrieveAttributes)
            result_set = []
            while 1:
                    result_type, result_data = l.result(ldap_result_id, 0)
                    if (result_data == []):
                            break
                    else:

                            if result_type == ldap.RES_SEARCH_ENTRY:
                                    result_set.append(result_data)
            print result_set
    except ldap.LDAPError, e:
            print e

You should extract desired part from result_set , for example:

result_set[0][0][1]['memberUid']

and print it with any manner you like:

from pprint import pprint
pprint(result_set[0][0][1]['memberUid'])

or

print('\n'.join(name for name in result_set[0][0][1]['memberUid']))

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