6

I get a malformed string error.

Here is my testings

>>> 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
DSM
  • 342,061
  • 65
  • 592
  • 494
user1513192
  • 1,123
  • 4
  • 17
  • 29

1 Answers1

8

From the ast.literal_eval docs:

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

Concatenation using + isn't included within that: it's not a literal expression, it's a call to str.__add__. It's the same reason 1+1 or "hello".upper() wouldn't work.

David Robinson
  • 77,383
  • 16
  • 167
  • 187