将.text文件转.xls文件

student.txt

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

txt_to_xls.py

import json
from openpyxl import Workbook


def txt_to_xlsx(filename):
    file = open(filename, 'r', encoding='UTF-8')
    file_cintent = json.load(file, encoding='UTF-8')
    print(file_cintent)
    workbook = Workbook()
    worksheet = workbook.worksheets[0]
    for i in range(1, len(file_cintent) + 1):
        worksheet.cell(row=i, column=1).value = i
        for m in range(0, len(file_cintent[str(i)])):
            print(file_cintent[str(i)])
            worksheet.cell(row=i, column=m + 2).value = file_cintent[str(i)][m]
    workbook.save(filename='student.xls')


if __name__ == '__main__':
    txt_to_xlsx('student.txt')

猜你喜欢

转载自blog.csdn.net/qq_42029527/article/details/83500939