Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

2
  • I see. This seems to be potentially dangerous as you say: myString = "\"\ndoBadStuff()\n\"", print(ast.literal_eval('"' + myString + '"')) seems to try to run code. How is ast.literal_eval any different/safer than eval? Commented Oct 26, 2010 at 4:05
  • 10
    @dln385: literal_eval never executes code. From the documentation, "This can be used for safely evaluating strings containing Python expressions from untrusted sources without the need to parse the values oneself." Commented Oct 26, 2010 at 4:16