Plain text file student.txt student information, which the content (including the braces) are as follows:
{ "1": [ "Joe Smith", 150,120,100], "2": [ "John Doe", 90,99,95], "3": [ "Wang Wu", 60,66,68]}
Please write the above student.xls file, as shown below:
-
#!/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')