How to get string objects instead of Unicode from JSON?

前端 未结 21 1200
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 14:43

I\'m using Python 2 to parse JSON from ASCII encoded text files.

When loading these files with either json or simplejson, all my

21条回答
  •  时光说笑
    2020-11-22 15:18

    Just use pickle instead of json for dump and load, like so:

        import json
        import pickle
    
        d = { 'field1': 'value1', 'field2': 2, }
    
        json.dump(d,open("testjson.txt","w"))
    
        print json.load(open("testjson.txt","r"))
    
        pickle.dump(d,open("testpickle.txt","w"))
    
        print pickle.load(open("testpickle.txt","r"))
    

    The output it produces is (strings and integers are handled correctly):

        {u'field2': 2, u'field1': u'value1'}
        {'field2': 2, 'field1': 'value1'}
    

提交回复
热议问题