Python excel file operations, programming exercises instance seventy-five

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:

  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')

Guess you like

Origin www.cnblogs.com/valorchang/p/11460218.html