Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 914835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:47:03+00:00 2026-05-15T17:47:03+00:00

I have been working to build a complex data structure which would return a

  • 0

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.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:47:04+00:00Added an answer on May 15, 2026 at 5:47 pm

    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}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working with a Windows Mobile 6 app that is built with
I have been working on a Silverlight app that consumes a WCF service. [on
I've been working on importing XML into an iPad Core Data application. I have
I have been working on this program but everytime I attempt to build, I
So I have been working with Wordpress 3.x and trying to build a custom
I have been working on an app all day which has been working fine
I have been working the whole day to try to build my app on
I have been working through a tutorial ( http://glacialflame.com/category/tutorial/ ) to build an Isometric
I'm a beginner but have been working really hard to build this application. Working
I am using C# .net 3.5 to build an application. I have been working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.