valorchang

纯文本文件 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.  

  5. import xlwt,json

  6.  

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

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

  9.    workbook = xlwt.Workbook()

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

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

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

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

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

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

分类:

技术点:

相关文章:

  • 2018-03-20
  • 2021-10-17
  • 2021-11-03
  • 2021-11-03
  • 2021-11-17
  • 2021-11-04
  • 2021-11-07
猜你喜欢
  • 2020-02-02
  • 2021-08-06
  • 2021-09-13
  • 2021-11-03
  • 2021-12-01
  • 2021-09-15
  • 2018-08-30
相关资源
相似解决方案