Dan
Dan

Reputation: 5173

What is the best Pythonic way to parse this data?

I'm newer to Python and am trying to find the most Pythonic way to parse a response from an LDAP query. So far what I have works but I'd like to make it neater if possible. My response data is this:

"[[('CN=LName\\, FName,OU=MinorUserGroup,OU=MajorUserGroup,DC=my,DC=company,DC=com', {'department': ['theDepartment'], 'mail': ['[email protected]']})]]"

Out of that data I'm really only interested in the fields within the {} so that I can throw it into a dictionary...

"department:theDepartment,mail:[email protected]"

What I'm doing now feels (and looks) really brute-force but works. I've added in extra commenting and output results based on what each step is doing to try and elaborate on this mess.

#Original String
#"[[('CN=LName\\, FName,OU=MinorUserGroup,OU=MajorUserGroup,DC=my,DC=company,DC=com', {'department': ['theDepartment'], 'mail': ['[email protected]']})]]"

#split at open {, take the latter half
myDetails = str(result_set[0]).split('{') 
#myDetails[1] = ["'department': ['theDepartment'], 'mail': ['[email protected]']})]]"]

#split at close }, take the former half
myDetails = str(myDetails[1]).split('}') 
#myDetails[0] = ["'department': ['theDepartment'], 'mail': ['[email protected]']"]

#split at comma to separate the two response fields
myDetails = str(myDetails[0]).split(',') 
#myDetails = ["'department': ['theDepartment']","'mail': ['[email protected]']"]

#clean up the first response field
myDetails[0] = str(myDetails[0]).translate(None, "'").translate(None," [").translate(None,"]") 
#myDetails[0] = ["department:theDepartment"]

#clean up the second response field
myDetails[1] = str(myDetails[1]).translate(None," '").translate(None, "'").translate(None,"[").translate(None,"]")
#myDetails[1] = ["mail:[email protected]"]

While I'm a big fan of "if it ain't broke, don't fix it" I'm a bigger fan of efficiency.

EDIT This ended up working for me per the accepted answer below by @Mario

myUser = ast.literal_eval(str(result_set[0]))[0][1] 
myUserDict = { k: v[0] for k, v in myUser.iteritems() }

Upvotes: 1

Views: 1170

Answers (2)

Mario
Mario

Reputation: 2505

Trusting your input and counting on its strict regularity, this will parse your example data and produce what it is you're expecting:

import ast

ldapData = "[[('CN=LName\\, FName,OU=MinorUserGroup,OU=MajorUserGroup,DC=my,DC=company,DC=com', {'department': ['theDepartment'], 'mail': ['[email protected]']})]]"

# Using the ast module's function is much safer than using eval. (See below!)
obj = ast.literal_eval(ldapData)[0][0]
rawDict = obj[1]
data = { k: v[0] for k, v in rawDict.iteritems() }

# The dictionary.
print data

The line using the curly brackets is called a dict comprehension.


Edit: Another user on this thread suggests using the ast.literal_eval function. I have to agree, after researching this. The eval function will execute any string. If the input was something like this, you'd have a big problem:

eval("__import__('os').system('rm -R *')") 

On the other hand, if this same string was parsed with the ast function, you would get an exception:

>>> import ast
>>> ast.literal_eval("__import__('os').system('rm -R *')")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/ast.py", line 80, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib64/python2.7/ast.py", line 79, in _convert
    raise ValueError('malformed string')
ValueError: malformed string
>>> 

Further discussion can be found here:

http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html

The module's documentation is here:

https://docs.python.org/2/library/ast.html

Upvotes: 5

Tim
Tim

Reputation: 43314

Considering this uses ast.literal_eval it's not perfect but it sure is cleaner

>>> import ast
>>> a = "[[('CN=LName\\, FName,OU=MinorUserGroup,OU=MajorUserGroup,DC=my,DC=company,DC=com', {'department': ['theDepartment'], 'mail': ['[email protected]']})]]"                                                                                                                                                                    
>>> ast.literal_eval(a)[0][0][1]
{'department': ['theDepartment'], 'mail': ['[email protected]']}
>>> type(ast.literal_eval(a)[0][0][1])                                                                                                                               
<type 'dict'>                                                                                                                                                        

Upvotes: 2

Related Questions