Using python's eval() vs. ast.literal_eval()?

前端 未结 6 1237
情深已故
情深已故 2020-11-21 06:39

I have a situation with some code where eval() came up as a possible solution. Now I have never had to use eval() before but, I have come across p

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 07:17

    I was stuck with ast.literal_eval(). I was trying it in IntelliJ IDEA debugger, and it kept returning None on debugger output.

    But later when I assigned its output to a variable and printed it in code. It worked fine. Sharing code example:

    import ast
    sample_string = '[{"id":"XYZ_GTTC_TYR", "name":"Suction"}]'
    output_value = ast.literal_eval(sample_string)
    print(output_value)
    

    Its python version 3.6.

提交回复
热议问题