+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2019-04(1)

2019-06(1)

2019-07(5)

2019-08(101)

2019-09(107)

合并多个csv文件到xlsx文件 python

发布于2021-01-15 21:05     阅读(631)     评论(0)     点赞(4)     收藏(0)


0

1

2

3

4

#   -*- coding:utf-8 -*-
"""
# @Author: Magician
# @Date: 2021-01-14 18:24:10
# @Description: 合并多个csv文件到一个excel表格中

# Copyright 2020 by Magician
"""
import openpyxl  # 读写.xlsx文件
import pandas as pd
import csv
import glob
import os

root_path = 'F:/数据处理/spesis/'

min_line = 34
max_line = 884
lines = max_line-min_line

dst_filepath = root_path + 'result/spesis+.xlsx'
dst = openpyxl.Workbook()
dst_sheet = dst.active
dst_sheet.title="Sheet1"

csv_list = glob.glob(root_path+'spesis+/*.csv')
print(u'共发现%s个CSV文件'%len(csv_list))
print(u'正在处理…')

write_value = []

for csv_num in csv_list: #循环读取同文件夹下的csv文件

    with open(csv_num,'r',encoding='utf-8') as csvfile:
        reader = csv.reader(csvfile)
        for csv_index,rows in enumerate(reader):
            if csv_index in range(min_line,max_line):
                write_value.append(rows)


for csv_num in csv_list: #循环读取同文件夹下的csv文件
    id = csv_num.replace('F:/数据处理/spesis/speisi-','').replace('.csv','')
    line = csv_list.index(csv_num)
    dst_sheet.cell(line+1,1,id)
    for i in range(lines):
        for j in range(4):        
            dst_sheet.cell(line+1,i+2,write_value[line][j])

dst.save(filename=dst_filepath)   
print(u'合并完毕!')

原文链接:https://blog.csdn.net/Magician0619/article/details/112639348

0

1

2

3

4

5

6

7

8



所属网站分类: 技术文章 > 博客

作者:从新来过

链接: https://www.pythonheidong.com/blog/article/771941/c52da398e43916e13ab2/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

4 0
收藏该文
已收藏

评论内容:(最多支持255个字符)