Python读取txt文本数据,写进excel中

因为项目需要,需要统计训练的loss和acc,写进excel中,用手统计太累了。

方法:读取文档后,采用split分割出数据,然后用列表保存,再将列表内容写进excel中。

txt文本内容如下所示:

Python读取txt文本数据,写进excel中_第1张图片

直接上代码,代码的value_title 需要自己手动更改:

# coding=UTF-8
import xlrd
import xlwt
from xlutils.copy import copy
import numpy as np 
 
def write_excel_xls(path, sheet_name, value):
    index = len(value)  # 获取需要写入数据的行数
    workbook = xlwt.Workbook()  # 新建一个工作簿
    sheet = workbook.add_sheet(sheet_name)  # 在工作簿中新建一个表格
    for i in range(0, index):
        for j in range(0, len(value[i])):
            sheet.write(i, j, value[i][j])  # 像表格中写入数据(对应的行和列)
    workbook.save(path)  # 保存工作簿
    print("xls格式表格写入数据成功!")
 
 
def write_excel_xls_append(path, value,j):
    index = len(value)  # 获取需要写入数据的行数
    print(index)
    workbook = xlrd.open_workbook(path)  # 打开工作簿
    sheets = workbook.sheet_names()  # 获取工作簿中的所有表格
    worksheet = workbook.sheet_by_name(sheets[0])  # 获取工作簿中所有表格中的的第一个表格
    # rows_old = worksheet.nrows  # 获取表格中已存在的数据的行数
    rows_old = 1  # 获取表格中已存在的数据的行数
    new_workbook = copy(workbook)  # 将xlrd对象拷贝转化为xlwt对象
    new_worksheet = new_workbook.get_sheet(0)  # 获取转化后工作簿中的第一个表格
    for i in range(0, index):
        print(i)
        print(value[i])
        new_worksheet.write(i+rows_old, j,  str(value[i]))  # 追加写入数据,注意是从i+rows_old行开始写入
    new_workbook.save(path)  # 保存工作簿
    print("xls格式表格【追加】写入数据成功!")
 
 
def read_excel_xls(path):
    workbook = xlrd.open_workbook(path)  # 打开工作簿
    sheets = workbook.sheet_names()  # 获取工作簿中的所有表格
    worksheet = workbook.sheet_by_name(sheets[0])  # 获取工作簿中所有表格中的的第一个表格
    for i in range(0, worksheet.nrows):
        for j in range(0, worksheet.ncols):
            print(worksheet.cell_value(i, j), "\t", end="")  # 逐行逐列读取数据
        print()
 
 
 


if __name__ == "__main__":
    file = open("MyNet.txt")
    Train_ET = []
    Train_TC = []
    Train_WT = []
    Valid_ET = []
    Valid_TC = []
    Valid_WT = []
    Train_loss = []
    Valid_loss = []
    for idx,line in enumerate(file.readlines()):
        if idx%2 != 0:
            Train_ET.append(line.split()[1])
            Train_TC.append(line.split()[3])
            Train_WT.append(line.split()[5])
            Valid_ET.append(line.split()[7])
            Valid_TC.append(line.split()[9])
            Valid_WT.append(line.split()[11])
        else:
            Train_loss.append(line.split()[2])
            Valid_loss.append(line.split()[4])

    # print(Train_ET)
    # print(Train_TC)
    # print(Train_WT)
    # print(Valid_ET)
    # print(Valid_TC)
    # print(Valid_WT)
    # print(Train_loss)
    # print(Valid_loss)

    book_name_xls = '测试值.xls'
    
    sheet_name_xls = '测试值表'
    
    value_title = [["Train_ET", "Train_TC", "Train_WT", "Valid_ET", "Valid_TC","Valid_WT","Train_loss","Valid_loss",],]

    write_excel_xls(book_name_xls, sheet_name_xls, value_title)
    write_excel_xls_append(book_name_xls, Train_ET, 0)
    write_excel_xls_append(book_name_xls, Train_TC, 1)
    write_excel_xls_append(book_name_xls, Train_WT, 2)
    write_excel_xls_append(book_name_xls, Valid_ET, 3)
    write_excel_xls_append(book_name_xls, Valid_TC, 4)
    write_excel_xls_append(book_name_xls, Valid_WT, 5)
    write_excel_xls_append(book_name_xls, Train_loss, 6)
    write_excel_xls_append(book_name_xls, Valid_loss, 7)

 最后生成的结果,很完美。Python读取txt文本数据,写进excel中_第2张图片

 

你可能感兴趣的:(蓝桥杯Python刷题记录,excel,python,数据分析)