Why does this line not work with ast.literal_eval

I get the wrong string error.

Here are my testimonies

>>> eval("'Hello:: '+'fdsfds'") 'Hello:: fdsfds' >>> import ast >>> ast.literal_eval("'Hello:: '+'fdsfds'") Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> ast.literal_eval("'Hello:: '+'fdsfds'") File "C:\Python27\lib\ast.py", line 80, in literal_eval return _convert(node_or_string) File "C:\Python27\lib\ast.py", line 79, in _convert raise ValueError('malformed string') ValueError: malformed string 
+4
source share
1 answer

From ast.literal_eval docs :

A string or node can contain only the following literary Python structures: strings, numbers, tuples, lists, dicts, booleans, and None.

Concatenation using + not included in this: it is not a literal expression, it is a call to str.__add__ . For the same reason, 1+1 or "hello".upper() will not work.

+6
source

All Articles