[英]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.