Python使用eval强制转换字符串为字典时报错:File "<string>", line 1, in <module> NameError: name 'nan' is not defined

  文本中保存的内容为:

{
    'QQQ': [0.067, 0.167, 0.2, 0.033, 0.233, 0.267, 0.1, 0.133], 
    'TTT': [0.5, 0.375, 0.25, 0.3, 0.6, 0.333, 0.857, 0.636, 0.667, 0.556]
}

   用eval转换为字符串时报错:

File "test.py", line 43, in d1
    data = eval(infile.readline())
  File "<string>", line 1, in <module>
NameError: name 'nan' is not defined

  解决:

globals = {
    'nan': 0   
}

# -----------
data = eval(infile.readline(), globals)

 

  参考资料:

  Python中,用eval强制将字符串转换为字典变量时候出错:NameError: name ‘null’ is not defined

 

posted @ 2014-08-28 15:58  zifenger  阅读(20442)  评论(1编辑  收藏  举报