题目来自:Python 练习册。题目1.8: 将纯文本文件 student.txt为学生信息,,写到 student.xls 文件内。
题目描述
题目1.8: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:
{
"1":["张三",150,120,100],
"2":["李四",90,99,95],
"3":["王五",60,66,68]
}
请将上述内容写到 student.xls 文件中,如下图所示:

student.xls
这个题使用到文件读取,数据读取,Xls文件输出三部分内容。
文件读取
使用open()函数
1 2
| with open('student.txt') as f: content = f.read()
|
数据读取
xls文件输出
1 2 3 4 5 6 7 8
| file = xlwt.Workbook()
table = file.add_sheet('test') for row, i in enumerate(list(d)): table.write(row, 0, i) for col, j in enumerate(d[i]): table.write(row, col + 1, j) file.save('student.xls')
|
参考代码
student.txt 可以在这里下载~ http://cdn.lifeiyang.cn/blog/student.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import os import json import xlwt
filepath = 'D:'
os.chdir(filepath)
with open('student.txt') as f: content = f.read()
d = json.loads(content) file = xlwt.Workbook()
table = file.add_sheet('test') for row, i in enumerate(list(d)): table.write(row, 0, i) for col, j in enumerate(d[i]): table.write(row, col + 1, j) file.save('student.xls')
|
未找到相关的 Issues 进行评论
请联系 @mmmwhy 初始化创建