python小程序-0014

来源:互联网 发布:java大牛的博客 编辑:程序博客网 时间:2024/06/05 06:24

第14题:纯文本文件 numbers.txt, 里面的内容(包括方括号)如下所示:

[
[1, 82, 65535],
[20, 90, 13],
[26, 809, 1024]
]
请将上述内容写到 numbers.xls 文件中,如下图所示:
这里写图片描述

#!/usr/bin/env python3# -*- coding : utf-8 -*-import jsonimport xlsxwriterfrom collections import OrderedDictdef txt_xls_numbers(txtpath):    workbook = xlsxwriter.Workbook('numbers.xls')    worksheet = workbook.add_worksheet('numbers')    with open(txtpath,'r') as f:        txtdata = json.load(f)    #print(txtdata)    for row, i in enumerate(txtdata):        for col, j in enumerate(i):            worksheet.write(row,col,j)    workbook.close()if __name__ == "__main__":    txtpath = input('Please input txt file path:')    txt_xls_numbers(txtpath)