美文网首页
str转dict时报错:File "", line 1, in

str转dict时报错:File "", line 1, in

作者: YNSeasons | 来源:发表于2018-11-21 15:52 被阅读0次

    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 "<string>", line 1, in <module>

    NameError: name 'true' is not defined

      解决:

    globals = {

        'true': 0 

    }

    # -----------

    data = eval(a, globals)

    若继续报错:

    File "test.py", line 43, in d1

        data = eval(a)

      File "<string>", line 1, in <module>

    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

          本文链接:https://www.haomeiwen.com/subject/djecqqtx.html