Convert unicode string dictionary into dictionary in python

前端 未结 5 1824

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

    I was getting unicode error when I was reading a json from a file. So this one worked for me.

    import ast
    job1 = {}
    with open('hostdata2.json') as f:
      job1= json.loads(f.read())
    
    f.close()
    
    #print type before converting this from unicode to dic would be 
    
    print type(job1)
    job1 =  ast.literal_eval(job1)
    print "printing type after ast"
    print type(job1)
    # this should result 
    
    for each in job1:
     print each
    print "printing keys"
    print job1.keys()
    print "printing values"
    print job1.values()
    

提交回复
热议问题