当试图解析一个空字符串时,我得到一个SyntaxError
. 为什么它会引发与解析 a 不同的错误'foo'
?ast.literal_eval
仅在来源中ValueError
被明确提出。
In [1]: import ast
In [2]: ast.literal_eval('foo')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-d8695a7c4a9f> in <module>()
----> 1 ast.literal_eval('foo')
/usr/lib/python2.7/ast.pyc in literal_eval(node_or_string)
78 return left - right
79 raise ValueError('malformed string')
---> 80 return _convert(node_or_string)
81
82
/usr/lib/python2.7/ast.pyc in _convert(node)
77 else:
78 return left - right
---> 79 raise ValueError('malformed string')
80 return _convert(node_or_string)
81
ValueError: malformed string
In [3]: ast.literal_eval('')
File "<unknown>", line 0
^
SyntaxError: unexpected EOF while parsing