2018-07-25

关于Python2.7里把中文写入json文件

1.设置文件格式
2.设置数据编码

# -*- coding: utf-8 -*-
import json,codecs

a = u'数据'
c = []
b = {}

b['name'] = a
c.append(b)

with codecs.open('a.json','wb','utf-8') as file:
    output = json.dumps(c, ensure_ascii=False)
    file.write(output)
    # file.write(json.dumps(a))
    # e = json.dumps(json.loads(a), ensure_ascii = False)
    # file.write(e)

file = codecs.open('b.json','wb','utf-8')

output = json.dumps(c, ensure_ascii=False)
file.write(output)

你可能感兴趣的:(2018-07-25)