我有4个不同的程序,读取4个不同类型的CSV文件,以获得相同格式的数据.我的目标是将每个程序的数据帧导出到一个单独的EXCEL工作簿中,放在4个单独的工作表上.每次我编写一个数据帧以超越它时,它都会覆盖刚刚导出到那里的数据.
这是我拥有的代码,每个程序都是相同的:
rowPos = 1
with pd.ExcelWriter(f"Y:\HedgeFundRecon\JAron\Output\JAronOutput.xlsx", datetime_format='mm/dd/yy') as writer:
for item in df_list:
item.to_excel(writer, sheet_name='JAron', startrow=rowPos)
rowPos += (len(item) + 2)
我将代码改为这样,以判断文件是否不存在,然后写入文件,但如果它确实存在,则覆盖工作表:
rowPos = 1
if(os.path.exists(path)):
with pd.ExcelWriter(path, engine='openpyxl', mode='a', if_sheet_exists='replace', datetime_format='dd/mm/yyyy') as writer:
for item in df_list:
item.to_excel(writer, sheet_name='JAron', startrow=rowPos)
rowPos += (len(item) + 2)
else:
with pd.ExcelWriter(path, engine='openpyxl', mode='w', date_format='dd/mm/yyyy') as writer:
for item in df_list:
item.to_excel(writer, sheet_name='JAron', startrow=rowPos)
rowPos += (len(item) + 2)
我将如何更改此代码以阻止数据被覆盖?我try 将模式设置为追加,但xlsxWriter不支持该模式.有什么建议吗?