不熟悉 python,想用 python 把 xls 文件转成 json 格式,用了下面的代码,在 python shell 能正常输出中文,可是一保存到文件里就是\u 的形式,环境为 win7 x64,python 3.6,不知道要怎么解决
import xlrd
from collections import OrderedDict
import json
import codecs
wb = xlrd.open_workbook('1.xls')
convert_list = []
sh = wb.sheet_by_index(0)
title = sh.row_values(0)
for rownum in range(1, sh.nrows):
rowvalue = sh.row_values(rownum)
single = OrderedDict()
for colnum in range(0, len(rowvalue)):
print(title[colnum], rowvalue[colnum])
single[title[colnum]] = rowvalue[colnum]
convert_list.append(single)
j = json.dumps(convert_list)
with codecs.open('file.json',"w","utf-8") as f:
f.write(j)
1
cfans1993 OP 找到问题了, 原来是 json_dumps 的原因,需加加上 ensure_ascii=False
json.dumps(convert_list,ensure_ascii=False,indent=2) |