working with ldap files

Tim Chase python.list at tim.thechases.com
Fri Sep 1 10:45:31 EDT 2006


> I have this string on a field
> CN=pointhairedpeoplethatsux,OU=Groups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com;CN=pointhairedboss,OU=Groups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com
> this string is all the groups one user has membership.
> So what I am trying to do.
> read this string
> and extract only the CNs
> 
> like
> 
> pointhairdepeoplethatsux,pointhairedboss

 >>> s = 
"CN=pointhairedpeoplethatsux,OU=Groups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com;CN=pointhairedboss,OU=Groups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com"
 >>> pieces = sum([p.split(';') for p in s.split(',')], [])
 >>> pieces
['CN=pointhairedpeoplethatsux', 'OU=Groups', 'OU=Hatepeople', 
'OU=HR', 'DC=fabrika', 'DC=com', 'CN=pointhairedboss', 
'OU=Groups', 'OU=Hatepeople', 'OU=HR', 'DC=fabrika', 'DC=com']
 >>> pieces = sum([p.split(';') for p in s.split(',')], [])
 >>> cns = [piece[3:] for piece in pieces if piece.startswith('CN=')]
 >>> cns
['pointhairedpeoplethatsux', 'pointhairedboss']


The process basically splits on commas, then splits each of those 
pieces on semi-colons, then flattens the list-of-lists into a 
single list of all the pieces (the flattening is done by abusing 
sum() so there may be better, more elegant ways of doing that). 
Once you have the flattened list of pieces, you can then just 
check for the ones that start with "CN=" and extract the bits of 
them that you need.

-tkc







More information about the Python-list mailing list