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)

---------------------

作者:兔八哥2009

来源:CSDN

原文:https://blog.csdn.net/qq_39708579/article/details/79377446

版权声明:本文为博主原创文章,转载请附上博文链接!

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