Python使用eval强制转换字符串为字典时报错:File "", line 1, in 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

上一篇:Python解决NameError: name 'reload' is not defined的问题


下一篇:Mongodb副本集--Out of memory: Kill process 37325 (mongod)