Python excel文件操作,编程练习题实例七十五

纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:

{ "1":["张三",150,120,100], "2":["李四",90,99,95], "3":["王五",60,66,68] }

请将上述内容写到 student.xls 文件中,如下图所示:

  1. #!/usr/bin/python

  2. # -*- coding: utf-8 -*-

  3. from collections import OrderedDict

  4. import xlwt,json

  5. with open('Python3v\python3v\test\student.txt','r', encoding="utf-8") as f:

  6.    data = json.load(f, object_pairs_hook=OrderedDict)

  7.    workbook = xlwt.Workbook()

  8.    sheet1 = workbook.add_sheet('student', cell_overwrite_ok=True)

  9.    for index, (key, values) in enumerate(data.items()):

  10.        sheet1.write(index, 0, key)

  11.        for i, value in enumerate(values):

  12.            sheet1.write(index, i+1, value)

  13.    workbook.save('Python3v\python3v\test\student.xls')

猜你喜欢

转载自www.cnblogs.com/valorchang/p/11460218.html