json文件转为str出现中文乱码问题
原来的代码是这样的
with open(fileName,'w',encoding='utf-8') as f:
json.dump(AllResume,f)
当然用
temp = json.dumps(AllResume)
with open(fileName,'w',encoding='utf-8') as f:
f.write(temp)
f.close()
得到的也是一样的结果。
出现的结果如下:
这是因为json.dumps或者json.dump 序列化时对中文默认使用的ascii编码,想输出真正的中文需要指定ensure_ascii=False:
with open(fileName,'w',encoding='utf-8') as f:
json.dump(AllResume,f,ensure_ascii=False)
此时得到的结果才是: