简体   繁体   English

Python + CSV + xlsx + Pandas:将多个CSV文件合并为一个多页CSV

[英]Python + CSV + xlsx + Pandas : Merging multiple CSV files into one multisheet CSV

I want to produce a multisheet CSV file from a multisheet xlsx file. 我想从多页xlsx文件生成多页CSV文件。 For that I wrote this code: 为此,我编写了以下代码:

xls = xlrd.open_workbook(r'Smallys ORDER.xlsx', on_demand=True)
df_list = []

names = xls.sheet_names()
names.remove('EVENT')

for name in names:
    prod = pd.read_excel('Smallys ORDER.xlsx', name, index_col=None)
    prod.to_csv(name + '.csv', encoding='utf-8', index=False) 
    df_list.append(prod)

df_final = pd.DataFrame()  

for df in df_list:
    df_final.append(df)

df_final.to_csv('smallys.csv', encoding='utf-8', index=False)

It successfully converts the individual xlsx sheets to csv files. 它成功地将各个xlsx工作表转换为csv文件。 But cannot produce the multisheet csv. 但无法生成多页csv。

this print(df_final) outputs this : print(df_final)输出以下内容:

Empty DataFrame
Columns: []
Index: []

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html mentions that the append method returns a new object. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html提到append方法将返回一个新对象。 So it has to be stored into a variable. 因此,必须将其存储到变量中。 Since you are not storing, your df_final is always empty. 由于未存储,因此df_final始终为空。 Try adding df_final = df_final.append(df) 尝试添加df_final = df_final.append(df)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM