纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:
{ "1":["张三",150,120,100], "2":["李四",90,99,95], "3":["王五",60,66,68] }
请将上述内容写到 student.xls 文件中,如下图所示:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from collections import OrderedDict
import xlwt,json
with open(‘Python3v\python3v\test\student.txt‘,‘r‘, encoding="utf-8") as f:
data = json.load(f, object_pairs_hook=OrderedDict)
workbook = xlwt.Workbook()
sheet1 = workbook.add_sheet(‘student‘, cell_overwrite_ok=True)
for index, (key, values) in enumerate(data.items()):
sheet1.write(index, 0, key)
for i, value in enumerate(values):
sheet1.write(index, i+1, value)
workbook.save(‘Python3v\python3v\test\student.xls‘)
原文:https://www.cnblogs.com/valorchang/p/11460218.html