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

  • Home
  • SEARCH
  • 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 254501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:51:06+00:00 2026-05-11T21:51:06+00:00

How can I convert the str representation of a dictionary, such as the following

  • 0

How can I convert the str representation of a dictionary, such as the following string, into a dict?

s = "{'muffin' : 'lolz', 'foo' : 'kitty'}"

I prefer not to use eval. What else can I use?

  • 1 1 Answer
  • 0 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-11T21:51:07+00:00Added an answer on May 11, 2026 at 9:51 pm

    You can use the built-in ast.literal_eval:

    >>> import ast
    >>> ast.literal_eval("{'muffin' : 'lolz', 'foo' : 'kitty'}")
    {'muffin': 'lolz', 'foo': 'kitty'}
    

    This is safer than using eval. As its own docs say:

    >>> help(ast.literal_eval)
    Help on function literal_eval in module ast:
    
    literal_eval(node_or_string)
        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.
    

    For example:

    >>> eval("shutil.rmtree('mongo')")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in <module>
      File "/opt/Python-2.6.1/lib/python2.6/shutil.py", line 208, in rmtree
        onerror(os.listdir, path, sys.exc_info())
      File "/opt/Python-2.6.1/lib/python2.6/shutil.py", line 206, in rmtree
        names = os.listdir(path)
    OSError: [Errno 2] No such file or directory: 'mongo'
    >>> ast.literal_eval("shutil.rmtree('mongo')")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/opt/Python-2.6.1/lib/python2.6/ast.py", line 68, in literal_eval
        return _convert(node_or_string)
      File "/opt/Python-2.6.1/lib/python2.6/ast.py", line 67, in _convert
        raise ValueError('malformed string')
    ValueError: malformed string
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I convert a string that describes an object into a JSON string
How can I convert a double into a String in Java for Mobile phones?
How can I convert a string into uppercase in Python? When I tried to
How can I convert a String array into an int array in java? I
I'm converting the following string to it's unsigned integer representation: str = 'x00x00x00x00x00x00x01xFF' I
How can I convert an str to a float ? "545.2222" -> 545.2222 Or
How can I convert a C++ std::string object into a Ruby VALUE object? I
I receive TypeError: Can't convert 'float' object to str implicitly while using Gambler.pot +=
So I know I can convert a string to a hashcode simply by doing
we can convert the dictionary to kw using **kw but if I want kw

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.