2

I have been working to build a complex data structure which would return a dictionary. Currently that class return string object of the form

{
   cset : x,    
   b1   : y,
   b2   : z,    
   dep  : {    
              cset : x1,
              b1   : y1,    
              b2   : z1,    
              dep  : {    
                         cset : x2,     
                         b1   : y2,    
                         b2   : z2,    
                         dep  : <same as above.it  recurses few more levels>                         
                         ...  
                     }
        }
    }

I want to convert this whole string object into dictionary. I read on one of the articles to use pickle module, but I don't want to serialize it into some file and use it.

Ref : http://bytes.com/topic/python/answers/36059-convert-dictionary-string-vice-versa

I am looking for some other neater ways of doing it, if possible.

Would be great to know any such ways.

2
  • 2
    Pro tip #1: Go back and accept answers to your previous questions to avoid looking like a jerk. (The check mark is next to the reply that best answers your question) Commented Jul 14, 2010 at 18:54
  • 1
    @Wayne : I did that, didn't know about that earlier, Thank you Commented Jul 14, 2010 at 19:20

4 Answers 4

8

Don't use eval. If you are sure that the string will always contain a valid Python dict, use ast.literal_eval. This works pretty much like eval, but it only evaluates if the expression is a valid dict,list, etc. and throws an exceptions if it isn't. This is way safer than trying to evaluate strings that may contain arbitrary code at runtime.

From the docs:

Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

This can be used for safely evaluating strings containing Python expressions from untrusted sources without the need to parse the values oneself.

Code Example:

>>> import ast
>>> ast.literal_eval("1+1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/ast.py", line 68, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib/python2.6/ast.py", line 67, in _convert
    raise ValueError('malformed string')
ValueError: malformed string
>>> ast.literal_eval("\"1+1\"")
'1+1'
>>> ast.literal_eval("{'a': 2, 'b': 3, 3:'xyz'}")
{'a': 2, 3: 'xyz', 'b': 3}
Sign up to request clarification or add additional context in comments.

Comments

1

If you are careful enough to keep the code valid python, you could use eval:

eval(yourlongstring)

Comments

0

Is that the actual data format? Do you have any flexibility?

It's very nearly JSON, which is a standard data format for which Python has native libraries for reading and writing (serialising and deserialising). However, to make it valid JSON both the keys and values would need to be surrounded by quotes:

"cset" : "x",
"b1"   : "y"

and so on.

Comments

-1

You probably do want to use Pickle:

http://docs.python.org/library/pickle.html#pickle.dumps

It can also return the value as a string, rather than writing to a file.

2 Comments

always better to use standard libraries if possible!
isnt ast module part of the standard library now ?

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.