第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:
{ "1":["张三",150,120,100], "2":["李四",90,99,95], "3":["王五",60,66,68]}
请将上述内容写到 student.xls 文件中,如下图所示:
import xlwt
import json
with open('student.txt') as f:
content = f.read()
wb = xlwt.Workbook()
ws = wb.add_sheet('student')
json = json.loads(content)
i = 0
for con in json:
values = json.get(con)
ws.write(i, 0, con)
j = 1
for value in values:
ws.write(i, j, value)
j += 1
i += 1
wb.save('stu.xls')
第 0015 题: 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示:
{ "1" : "上海", "2" : "北京", "3" : "成都"}
import json
import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('city')
with open('file\\city.txt') as f:
content = f.read()
json = json.loads(content)
i = 0
for con in json:
ws.write(i, 0, con)
ws.write(i, 1, json.get(con))
i += 1
wb.save('file\\city.xls')
第 0016 题: 纯文本文件 numbers.txt, 里面的内容(包括方括号)如下所示:
[ [1, 82, 65535], [20, 90, 13], [26, 809, 1024]]
请将上述内容写到 numbers.xls 文件中,如下图所示:
import json
import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('numbers')
with open('file\\num.txt') as f:
content = f.read()
json = json.loads(content)
i = 0
for con in json:
j = 0
for item in con:
print(item)
ws.write(i, j, item)
j += 1
i += 1
wb.save('file\\num.xls')