您的位置:首页 > 编程语言 > Python开发

Python练手项目0014

2017-02-16 15:52 429 查看
本项目采用的是https://github.com/Yixiaohan/show-me-the-code中所提供的练习项目,所有代码均为原创,转载请注明,谢谢。

问题描述:纯文本文件 student.txt为学生信息, 里面的内容转换到xls中

# -*- coding: utf-8 -*-

"""

Created on Wed Feb 15 13:45:43 2017

@author: sky

"""

import xlwt

import re

def txt2xls(input_path,output_name):

    book = xlwt.Workbook(encoding = 'utf-8', style_compression=0)

    sheet = book.add_sheet(output_name,cell_overwrite_ok = True)

    line = 0

    info = re.compile(r'\"(\d+)\":\[\"(.*?)\",(\d+),(\d+),(\d+)\]')

    with open(input_path,"r") as f:

        data = f.read()

    for x in info.findall(data):

        for i in range(len(x)):

            sheet.write(line,i,x[i])

        line+=1

    output_name = output_name + '.xls'

    book.save(output_name)

    

if __name__ == '__main__':
    txt2xls('student.txt','3')

xlwt用来生成一个excel文档,具体可以参考http://www.jb51.net/article/60510.htm

详细代码和结果,可以参考https://github.com/g8015108/exercise-for-python
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python