user1470034
user1470034

Reputation: 691

Python Alternative to .split() for generator/list

How can I add a new line after each object in my generator or list? I tried .join('\n') and that didn't work. Trying to split a newline won't work because that's not an attribute of the generator or list datatype.

My try with a generator = false to use a list using join:

a = conn.extend.standard.paged_search('cn = All.DL Div Controllers - National Group, ou = Distribution Lists, ou = Exchange, dc=google,dc=corpad,dc=net', '(objectClass=*)', attributes=['member'], generator=False)
b = map(str, a)         #Stringify each a from generator
b_str = '\n'.join(b) #Join the list of b with a newline
with open ('test.txt', 'w') as file:
    file.write(b_str) #Write to file

My output looks like this aaaa, aaaaa, aaaaaa,aaa,aaaaa,aaaaa

I want my output to look like this:

aaaa,
aaaaa,
aaaaaa,
aaa,
aaaaa,
aaaaa,

Here is my attempt using .split(), but it obviously wont' work as .split() is not an attribute of a generator or list datatype.

#a= conn.extend.standard.paged_search('cn = All.DL Div Controllers - National Group, ou = Distribution Lists, ou = Exchange, dc=google,dc=corpad,dc=net', '(objectClass=*)', attributes=['member'])
#for b in a.split('\n'):
#    with open ('test.txt', 'w') as file:
#        file.write (str(a))

The above is just an example the true output in the console including each print statement is the following:

Code:

national = conn.extend.standard.paged_search('cn = All.DL Div Controllers - National Group, ou = Distribution Lists, ou = Exchange, dc=google,dc=corpad,dc=net', '(objectClass=*)', attributes=['member'], generator=False)
print(national)

Output:

[{'raw_dn': b'CN=All.DL Div Controllers - National Group,OU=Distribution Lists,OU=Exchange,DC=google,DC=corpad,DC=net',
     'dn': 'CN=All.DL Div Controllers - National Group,OU=Distribution Lists,OU=Exchange,DC=google,DC=corpad,DC=net',
     'raw_attributes': {'member': [b'CN=gji9847,OU=U02562,OU=02562,DC=google,DC=corpad,DC=net',
                                   b'CN=exx8092,OU=U06032,OU=06032,DC=google,DC=corpad,DC=net',
                                   b'CN=EWR8386,OU=U09373,OU=09373,DC=google,DC=corpad,DC=net',
                                   b'CN=HXU9279,OU=U00704,OU=00704,DC=google,DC=corpad,DC=net',
                                   b'CN=KVG6693,OU=U25692,OU=25692,DC=google,DC=corpad,DC=net',
                                   b'CN=JCL6495,OU=U01246,OU=01246,DC=google,DC=corpad,DC=net',
                                   b'CN=RNE9520,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                                   b'CN=PCN5400,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                                   b'CN=WPM9271,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                                   b'CN=JKJ2421,OU=U01692,OU=01692,DC=google,DC=corpad,DC=net']},
     'attributes': {'member': ['CN=gji9847,OU=U02562,OU=02562,DC=google,DC=corpad,DC=net',
                               'CN=exx8092,OU=U06032,OU=06032,DC=google,DC=corpad,DC=net',
                               'CN=EWR8386,OU=U09373,OU=09373,DC=google,DC=corpad,DC=net',
                               'CN=HXU9279,OU=U00704,OU=00704,DC=google,DC=corpad,DC=net',
                               'CN=KVG6693,OU=U25692,OU=25692,DC=google,DC=corpad,DC=net',
                               'CN=JCL6495,OU=U01246,OU=01246,DC=google,DC=corpad,DC=net',
                               'CN=RNE9520,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                               'CN=PCN5400,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                               'CN=WPM9271,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                               'CN=JKJ2421,OU=U01692,OU=01692,DC=google,DC=corpad,DC=net']},
     'type': 'searchResEntry'}]

Code:

print (len(list(national)))

Output:

1

Code:

controllers = map(str, national)         #Stringify each controller from generator
print(controllers)

Output:

<map object at 0x000001BF3AF35080>

Code:

controllers_str = '\n'.join(controllers) #Join the list of controllers with a newline
print(controllers_str)

Output:

{'raw_dn': b'CN=All.DL Div Controllers - National Group,OU=Distribution Lists,OU=Exchange,DC=google,DC=corpad,DC=net',
     'dn': 'CN=All.DL Div Controllers - National Group,OU=Distribution Lists,OU=Exchange,DC=google,DC=corpad,DC=net',
     'raw_attributes': {'member': [b'CN=gji9847,OU=U02562,OU=02562,DC=google,DC=corpad,DC=net',
                                   b'CN=exx8092,OU=U06032,OU=06032,DC=google,DC=corpad,DC=net',
                                   b'CN=EWR8386,OU=U09373,OU=09373,DC=google,DC=corpad,DC=net',
                                   b'CN=HXU9279,OU=U00704,OU=00704,DC=google,DC=corpad,DC=net',
                                   b'CN=KVG6693,OU=U25692,OU=25692,DC=google,DC=corpad,DC=net',
                                   b'CN=JCL6495,OU=U01246,OU=01246,DC=google,DC=corpad,DC=net',
                                   b'CN=RNE9520,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                                   b'CN=PCN5400,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                                   b'CN=WPM9271,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                                   b'CN=JKJ2421,OU=U01692,OU=01692,DC=google,DC=corpad,DC=net']},
     'attributes': {'member': ['CN=gji9847,OU=U02562,OU=02562,DC=google,DC=corpad,DC=net',
                               'CN=exx8092,OU=U06032,OU=06032,DC=google,DC=corpad,DC=net',
                               'CN=EWR8386,OU=U09373,OU=09373,DC=google,DC=corpad,DC=net',
                               'CN=HXU9279,OU=U00704,OU=00704,DC=google,DC=corpad,DC=net',
                               'CN=KVG6693,OU=U25692,OU=25692,DC=google,DC=corpad,DC=net',
                               'CN=JCL6495,OU=U01246,OU=01246,DC=google,DC=corpad,DC=net',
                               'CN=RNE9520,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                               'CN=PCN5400,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                               'CN=WPM9271,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                               'CN=JKJ2421,OU=U01692,OU=01692,DC=google,DC=corpad,DC=net']},
     'type': 'searchResEntry'}

Upvotes: 0

Views: 108

Answers (2)

FHTMitchell
FHTMitchell

Reputation: 12157

ok so national is a list of length 1. To get the object we care about, all you need to do is national_dict = national[0]. I called it this since national_dict is a dictionary, not a string.

To print out a nicely formatted dictionary, one option is to pretty print

from pprint import pprint
pprint(national_dict, width=100)  # width defaults to 79

which outputs

{'attributes': {'member': ['CN=gji9847,OU=U02562,OU=02562,DC=google,DC=corpad,DC=net',
                           'CN=exx8092,OU=U06032,OU=06032,DC=google,DC=corpad,DC=net',
                           'CN=EWR8386,OU=U09373,OU=09373,DC=google,DC=corpad,DC=net',
                           'CN=HXU9279,OU=U00704,OU=00704,DC=google,DC=corpad,DC=net',
                           'CN=KVG6693,OU=U25692,OU=25692,DC=google,DC=corpad,DC=net',
                           'CN=JCL6495,OU=U01246,OU=01246,DC=google,DC=corpad,DC=net',
                           'CN=RNE9520,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                           'CN=PCN5400,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                           'CN=WPM9271,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                           'CN=JKJ2421,OU=U01692,OU=01692,DC=google,DC=corpad,DC=net']},
 'dn': 'CN=All.DL Div Controllers - National Group,OU=Distribution '
       'Lists,OU=Exchange,DC=google,DC=corpad,DC=net',
 'raw_attributes': {'member': [b'CN=gji9847,OU=U02562,OU=02562,DC=google,DC=corpad,DC=net',
                               b'CN=exx8092,OU=U06032,OU=06032,DC=google,DC=corpad,DC=net',
                               b'CN=EWR8386,OU=U09373,OU=09373,DC=google,DC=corpad,DC=net',
                               b'CN=HXU9279,OU=U00704,OU=00704,DC=google,DC=corpad,DC=net',
                               b'CN=KVG6693,OU=U25692,OU=25692,DC=google,DC=corpad,DC=net',
                               b'CN=JCL6495,OU=U01246,OU=01246,DC=google,DC=corpad,DC=net',
                               b'CN=RNE9520,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                               b'CN=PCN5400,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                               b'CN=WPM9271,OU=U01673,OU=01673,DC=google,DC=corpad,DC=net',
                               b'CN=JKJ2421,OU=U01692,OU=01692,DC=google,DC=corpad,DC=net']},
 'raw_dn': b'CN=All.DL Div Controllers - National Group,OU=Distribution Lists,OU=Exchange,DC=goog'
           b'le,DC=corpad,DC=net',
 'type': 'searchResEntry'}

Is this sort of what you're looking for?


In order to pretty print to a file

import pprint
formatted = pprint.pformat(national_dict, width=100)
with open(filename, 'w') as fw:
    fw.write(formatted)

Upvotes: 1

David Gaertner
David Gaertner

Reputation: 386

Can you replace each comma with ",\n"? b = str(a).replace(',',',\n')

Upvotes: 1

Related Questions