Json.dump failing with 'must be unicode, not str' TypeError

前端 未结 3 892
我在风中等你
我在风中等你 2021-02-05 10:10

I have a json file which happens to have a multitude of Chinese and Japanese (and other language) characters. I\'m loading it into my python 2.7 script using io.open

3条回答
  •  孤城傲影
    2021-02-05 10:18

    The JSON module handles encoding and decoding for you, so you can simply open the input and output files in binary mode. The JSON module assumes UTF-8 encoding, but can be changed using encoding attribute on the load() and dump() methods.

    with open('multiIdName.json', 'rb') as json_data:
        cards = json.load(json_data)
    

    then:

    with open("testJson.json", 'wb') as outfile:
        json.dump(cards, outfile, ensure_ascii=False)
    

    Thanks to @Antti Haapala, Python 2.x JSON module gives either Unicode or str depending on the contents of the object.

    You will have to add a sense check to ensure the result is a Unicode before writing through io:

    with io.open("testJson.json", 'w', encoding="utf-8") as outfile:
        my_json_str = json.dumps(my_obj, ensure_ascii=False)
        if isinstance(my_json_str, str):
            my_json_str = my_json_str.decode("utf-8")
    
        outfile.write(my_json_str)
    

提交回复
热议问题