0

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'}
13
  • 4
    @scharette python is clever enough to convert line endings automatically. OP, your code is writing controllers_str, not b_str. Is this intended? Commented Jul 9, 2018 at 15:45
  • After your edit, I doubt that what you have written isn't working. I see no reason why it shouldn't. Are you sure len(list(b)) > 1? Commented Jul 9, 2018 at 15:53
  • That's the issue my len(list) is only = to 1. Commented Jul 9, 2018 at 16:03
  • Wait, so your generator only produces a single element which looks like 'aaaa, aaaaa, aaaaaa,aaa,aaaaa,aaaaa'? Commented Jul 9, 2018 at 16:15
  • Apparently, how can I get that to line break after each comma? Or would I be better off reading that text file being produced and doing the editing there? I'm new to all of this. Commented Jul 9, 2018 at 18:01

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I think you're on to something. I can replace every DC = net', with DC = net', \n.
1

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)

2 Comments

You can also use print(..., file=f) and it will handle the newlines etc
It's been a while, I was out of my country all month disconnected. This is sort of what I was looking for, but instead of printing, I want the text file being wrote to display like the pretty print.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.