python写入中文json

Posted Circle-C

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python写入中文json相关的知识,希望对你有一定的参考价值。

Python2 写入带有中文的数据到json文件出错:

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

with io.open("test.json",'w',encoding="utf-8") as outfile:
    json.dump(cards, outfile, ensure_ascii=False)

解决:

 

# -*- coding:utf-8 -*-
import codecs
with codecs.open("test.json","w") as file:
    json.dump(my_list, file, indent=4, ensure_ascii=False)

以上是关于python写入中文json的主要内容,如果未能解决你的问题,请参考以下文章