读取csv文件,写入Excel的多个单元簿中

读取目录下的csv文件,写入同一个excel文件的不同的sheet中。

import xlsxwriter as xlwt
import os

to_file_name = ""
csv_path     = ""
workbook = xlwt.Workbook(to_file_name)  # 新建一个工作簿

def handle_line(line):
    return line[:-1].split(",")

for file_name in os.listdir(csv_path):
    name = "%s/%s" % (csv_path, file_name)
    datas = []
    with open(name, "r", encoding="utf-8") as f:
        for line in f:
            data = handle_line(line)
            datas.append(v)
    index = len(datas)  # 获取需要写入数据的行数
    sheet = workbook.add_worksheet(file_name[:-4])  # 在工作簿中新建一个表格
    for i in range(0, index):
        for j in range(0, len(datas[i])):
            sheet.write(i, j, datas[i][j])  # 像表格中写入数据(对应的行和列)
workbook.close()  # 保存工作簿
posted @ 2020-11-19 10:26  呓语i  阅读(164)  评论(0编辑  收藏  举报