Convert unicode string dictionary into dictionary in python

前端 未结 5 1837

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 02:51

    EDIT: Turns out my assumption was incorrect; because the keys are not wrapped in double-quote marks ("), the string isn't JSON. See here for some ways around this.

    I'm guessing that what you have might be JSON, a.k.a. JavaScript Object Notation.

    You can use Python's built-in json module to do this:

    import json
    result = json.loads(u"{'code1':1,'code2':1}")   # will NOT work; see above
    

提交回复
热议问题