Convert unicode string dictionary into dictionary in python

前端 未结 5 1826

I have unicode u\"{\'code1\':1,\'code2\':1}\" and I want it in dictionary format.

I want it in {\'code1\':1,\'code2\':1} format.

I

5条回答
  •  感情败类
    2020-12-08 03:06

    You can use built-in ast package:

    import ast
    
    d = ast.literal_eval("{'code1':1,'code2':1}")
    

    Help on function literal_eval in module ast:

    literal_eval(node_or_string)

    Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

提交回复
热议问题