使用ast.literal_eval转换字符串时出错

时间:2019-01-17 04:23:20

标签: python

我遇到以下错误。我无法弄清楚clonedRadardetails中出了什么问题。任何指导都会有所帮助。

import ast
clonedRadardetails = {'clonedRadar': u'42719619', 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}
v = ast.literal_eval(clonedRadardetails)

错误:

 File "astliteral.py", line 3, in <module>
    v = ast.literal_eval(clonedRadardetails)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 80, in literal_eval
    return _convert(node_or_string)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 79, in _convert
    raise ValueError('malformed string')
ValueError: malformed string

1 个答案:

答案 0 :(得分:-2)

摘自ast.literal_eval()的文档:

安全地评估表达式节点或包含Python表达式的字符串。提供的字符串或节点只能由以下Python文字结构组成:字符串,数字,元组,列表,字典,布尔值和None