0

Firstly I am using python 2.7.

What I am trying to achieve is to separate table data from a active directory lookup into "Firstname Lastname" items in an array, which I can later compare to a different array and see which users in the list do not match.

I run dsquery group domainroot -name groupname | dsget group -members | dsget user -fn -ln which outputs a list as such:

  fn               ln               
  Peter            Brill            
  Cliff            Lach          
  Michael          Tsu               
  Ashraf           Shah             
  Greg             Coultas          
  Yi               Li               
  Brad             Black            
  Kevin            Schulte          
  Raymond          Masters (Admin)  
  James            Rapp            
  Allison          Wurst            
  Benjamin         Hammel            
  Edgar            Cuevas           
  Vlad             Dorovic (Admin)        
  Will             Wang                        
dsget succeeded

Notice this list has both spaces before and after each data set.

The code I am using currently:

userarray = []
p = Popen(["cmd.exe"], stdin=PIPE, stdout=PIPE)
p.stdin.write("dsquery group domainroot -name groupname | dsget group -members | dsget user -fn -ln\n")
p.stdin.write("exit\n")
processStdout = p.stdout.read().replace("\r\n", "").strip("")[266:]
cutWhitespace = ' '.join(processStdout.split()).split("dsget")[0]
processSplit = re.findall('[A-Z][^A-Z]*', cutWhitespace)
userarray.append(processSplit)
print userarray

My problem is that when I split on the blank space and attempt to re-group them into "Firstname Lastname" when it hits the line in the list that has (Admin) the grouping gets thrown off because there is a third field. Here is a sample of what I mean:

['Brad ', 'Black ', 'Kevin ', 'Schulte ', 'Raymond ', 'Masters (', 'Admin) ', 'James ', 'Rapp ', 'Allison ', 'Wurst ',

I would appreciate any suggestions on how to group this better or correctly. Thanks!

2
  • a fast work around is to remove whitespace next to a parenthesis in a line, then split, then re group. but njzk2s answer is better! Commented Jul 2, 2015 at 21:01
  • Looks like the lines can be parsed by position. Use slicing to get the first 20 characters, left and right trim, then grab everything past 20 and left and right trim it as well. Commented Jul 2, 2015 at 21:11

2 Answers 2

1
# the whole file.
content = p.stdout.read()
# each line as a single string
lines = content.split()
# lets drop the header and the last line
lines = lines[1:-1]
# Notice how the last name starts at col 19
names = [(line[:19].strip(), line[19:].strip()) for line in lines]
print(names)
=> [('Peter', 'Brill'), ('Cliff', 'Lach'), ('Michael', 'Tsu'), ('Ashraf', 'Shah'), ('Greg', 'Coultas'), ('Yi', 'Li'), ('Brad', 'Black'), ('Kevin', 'Schulte'), ('Raymond', 'Masters (Admin)'), ('James', 'Rapp'), ('Allison', 'Wurst'), ('Benjamin', 'Hammel'), ('Edgar', 'Cuevas'), ('Vlad', 'Dorovic (Admin)'), ('Will', 'Wang')]

Now, if the column size change, just do index = lines[0].indexof('ln') before dropping the header and use that instead of 19

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

2 Comments

I needed to change the line to lines = content.split("\r\n") to parse the data into `('Firstname', 'Lastname')
stdout should also have a readlines() method, so lines = p.stdout.readlines() should work too
0

split has a maxsplit argument you so you can tell it to split only the first separator, so you could say:

cutWhitespace = ' '.join(processStdout.split(None,1)).split("dsget")[0]

on your sixth line to tell it to split no more than once.

Comments

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.