str转dict时报错:File "", line 1, in NameError: name 'true' is not defined

a = {
    '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(a)
  File "", line 1, in 
NameError: name 'true' is not defined

  解决:

globals = {
    'true': 0   
}

# -----------
data = eval(a, globals)
若继续报错:

File "test.py", line 43, in d1
    data = eval(a)
  File "", line 1, in 
NameError: name 'false' is not defined

  解决:

globals = {
    'true': 0,
    'false': 1   
}

# -----------
data = eval(a, globals)

你可能感兴趣的:(str转dict时报错:File "", line 1, in NameError: name 'true' is not defined)