python3在通过Json往文件里写内容时中文字符被转成unicode编码
import json
class file_open(object):
f = open('chooes_course.txt', 'r+', encoding='utf-8')
def write_f(self,info):
self.info = info
file_open.f.write(json.dumps(self.info))
def read_f(self):
print(file_open.f.read())
def __del__(self):
file_open.f.flush()
file_open.f.close()
members = {'张三': ['zs123', '56.8'], 'lisi': ['ls123', '47.2'], 'mologa': ['mologa123', '100']}
f1= file_open()
f1.write_f(members)
打开choose_course.txt显示
{"\u5f20\u4e09": ["zs123", "56.8"], "lisi": ["ls123", "47.2"], "mologa": ["mologa123", "100"]}
*如上 '张三' ==> '\u5f20\u4e09'
解决办法:
在dumps中加入参数 ensure_ascii=False
def write_f(self,info):
self.info = info
file_open.f.write(json.dumps(self.info,ensure_ascii=False))
即可解决
{"lisi": ["ls123", "47.2"], "张三": ["zs123", "56.8"], "mologa": ["mologa123", "100"]}100"]}
参考:https://www.2cto.com/kf/201411/351186.html
|