简体   繁体   English

如何从多个 csv 文件中读取数据并写入 Python 中的单个 Excel 表的同一张表

[英]How to read data from multiple csv files and write into same sheet of single Excel Sheet in Python

I want append multiple csv files data into same sheet of single excel sheet with one empty row between data.我想将 append 多个 csv 文件数据放入同一张 excel 工作表中,数据之间有一个空行。

1.csv 1.csv

ID  Currency    Val1    Val2        Month
101 INR     57007037.32 1292025.24  2021-03
102 INR     49171143.9  1303785.98  2021-02

2.csv 2.csv

ID  Currency    Val1    Val2        Month
103 INR     67733998.9  1370086.78  2020-12
104 INR     48838409.39 1203648.32  2020-11

Now I want to write into same sheet of excel sheet with one empty row like below.现在我想在同一张 excel 表中写入一个空行,如下所示。

output.xlsx output.xlsx

ID  Currency    Val1    Val2        Month
101 INR     57007037.32 1292025.24  2021-03
102 INR     49171143.9  1303785.98  2021-02

103 INR     67733998.9  1370086.78  2020-12
104 INR     48838409.39 1203648.32  2020-11

Error:错误:

在此处输入图像描述

1.csv: 1.csv:

ID;Currency;Val1;Val2;Month
101;INR;57007037.32;1292025.24;2021-03
102;INR;49171143.9;1303785.98;2021-02

2.csv: 2.csv:

ID;Currency;Val1;Val3;Month;Year
103;INR;67733998.9;1370086.78;2020-12;2020
104;INR;48838409.39;1203648.32;2020-11;2020

3.csv 3.csv

ID;Currency;Val2;Year
105;INR;34325309.92;2020
106;INR;18098469.39;2020
import pandas as pd
import numpy as np

dfs = []
files = ["1.csv", "2.csv", "3.csv"]

for csv in files:
    df = pd.read_csv(csv, delimiter=";")
    df = df.append(pd.DataFrame([[np.NaN] * df.shape[1]], columns=df.columns))
    dfs.append(df)

dfs = pd.concat(dfs).to_excel("output.xlsx", na_rep="", index=False)

在此处输入图像描述

Edit : problem of columns order编辑:列顺序问题

>>> df
    2019-01   2020-01   2020-09  ...   2021-03  2021-03.1   Name  id  currency
0  0.665912  0.140293  0.501259  ...  0.714760   0.586644    Ram   A       INR
1  0.217433  0.950174  0.618288  ...  0.699932   0.219194  Krish   A       INR
2  0.419540  0.788270  0.490949  ...  0.315056   0.312781  Sandy   A       INR
3  0.034803  0.335773  0.563574  ...  0.580068   0.949062  Dhanu   A       INR
>>> BASECOLS = ["id", "currency", "Name"]
>>> cols = BASECOLS + list(reversed(df.columns[~df.columns.isin(BASECOLS)]))
>>> df[cols]
  id currency   Name  2021-03.1   2021-03  ...  2020-09   2020-01   2019-01
0  A      INR    Ram   0.586644  0.714760  ...  0.501259  0.140293  0.665912
1  A      INR  Krish   0.219194  0.699932  ...  0.618288  0.950174  0.217433
2  A      INR  Sandy   0.312781  0.315056  ...  0.490949  0.788270  0.419540
3  A      INR  Dhanu   0.949062  0.580068  ...  0.563574  0.335773  0.034803

The pandas package makes this much easier in my opinion:在我看来pandas package让这变得容易多了:

import pandas as pd

files = [
    'path/to/file1.csv',
    'path/to/file2.csv',
    'path/to/file3.csv',
]


spreadsheet = pd.ExcelWriter('path/to/output.xlsx')

for file in files:
    sheet_name = file.split('.')[0]
    data = pd.read_csv(file)
    data.to_excel(spreadsheet, sheet_name=sheet_name, index=None)

spreadsheet.save()

I suggest use to use pandas.我建议使用 pandas。 It has an excellent xlsx writer thant can do the job for you very simple.它有一个出色的 xlsx 编写器,可以非常简单地为您完成这项工作。 Basically you have to initialize your excel writer then loop through csvs, read one by one and write to file.基本上你必须初始化你的 excel 写入器,然后遍历 csvs,一一读取并写入文件。 I suggest you to use pd.ExcelWriter so xlsx file will be touched only one time.我建议你使用pd.ExcelWriter这样 xlsx 文件只会被触及一次。 Also mode='a' lets you append sheets to existing excel file, remove it if you want to overwrite the entire file.此外, mode='a'允许您将 append 工作表添加到现有的 excel 文件,如果您想覆盖整个文件,请将其删除。 See docs .请参阅文档

import pandas as pd
with pd.ExcelWriter('output.xlsx', mode='a') as writer:
    #here you loop through csvs and load
    for csv in csvs:
        df = pd.read_csv(csv)
        df.to_excel(writer, sheet_name=csv)
import pandas as pd
import numpy as np
import os

try:

    spreadsheet = pd.ExcelWriter('/home/Report.xlsx',engine='xlsxwriter')
    for root, dirs, files in os.walk('/home/'):
        final_data=pd.DataFrame()
        for csv in files:
            df = pd.read_csv(csv)
            df1 = pd.DataFrame([[np.nan] * df.shape[1]], columns=df.columns) 
            final_data=final_data.append(df) 
            final_data=final_data.append(df1)
            final_data.to_excel(spreadsheet,na_rep="",sheet_name='report',index=False)
    
    spreadsheet.save()

except (RuntimeError, TypeError, NameError):
        print("Unable to load data into Excel Sheet")
        raise

I have similar requirement but i have to merge multiple csv into one xls with different tabs.我有类似的要求,但我必须将多个 csv 合并到一个带有不同选项卡的 xls 中。 csv are placed on blob storage and need to make and xls from them.any pointer will help csv 被放置在 blob 存储上,需要从中制作和 xls。任何指针都会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中将相同的数据从Excel工作表读取到文本文件 - How to read the same data from excel sheet to the textfile in Python 如何使用 python 从单个 excel 表中读取两个表? - How to read two tables from single excel sheet using python? 如何使用python从文件夹中的多个excel文件中读取具有“ mine”工作表名称的工作表? 我正在使用xlrd - how to read any sheet with the sheet name containing 'mine' from multiple excel files in a folder using python? i am using xlrd 如何从多个文件中提取相同的 excel 表? - How can I extract the same excel sheet from multiple files? 如何从 Python 中的 excel 工作表的每个选项卡中读取多个表格? - How to read multiple tables from each tab of an excel sheet in Python? PYTHON:从 csv 表读取 - PYTHON: Read from csv sheet 如何使用 Python 将数据从一个 Excel 工作表复制到同一工作簿的另一工作表? - How to copy data from One Excel sheet to another sheet for same Workbook Using Python? 如何读取多个Excel文件并将其加载到一张Excel工作表中 - How to read multiple excel files and load into one excel sheet 熊猫-将多个数据框写入单个Excel工作表 - Pandas - Write multiple dataframes to single excel sheet 如何通过python中的pandas导出单个csv文件的多个excel表 - How to export to multiple excel sheet of a single csv file through pandas in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM