简体   繁体   中英

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': ['theEmail@mycompany.com']})]]"

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:theEmail@mycompany.com"

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': ['theEmail@mycompany.com']})]]"

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

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

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

#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:theEmail@mycompany.com"]

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() }

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': ['theEmail@mycompany.com']})]]"

# 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

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': ['theEmail@mycompany.com']})]]"                                                                                                                                                                    
>>> ast.literal_eval(a)[0][0][1]
{'department': ['theDepartment'], 'mail': ['theEmail@mycompany.com']}
>>> type(ast.literal_eval(a)[0][0][1])                                                                                                                               
<type 'dict'>                                                                                                                                                        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM