编写器出现不支持的函数截断错误:close()

问题描述 投票:0回答:1
RA = pd.read_excel(file_path, header=0, sheet_name="Data")
pivot_df = pd.DataFrame(RA)
#Inserting Pivot
pivot = pivot_df[filter2].pivot_table(index=['a','b', 'c'], values=['Total1','Total2'], aggfunc='sum', margins=True, margins_name='Total')

#copy & pasting pivot to Summary sheet
writer = pd.ExcelWriter(file_path, engine='openpyxl', mode='a', if_sheet_exists='overlay')
pivot.to_excel(writer, sheet_name='Summary', index=True)

writer.close()
writer.handles = None

在 writer.close() 处,我收到错误 UnsupportedOperation: truncate。在一个系统上它工作正常,但在另一个系统上则不然。我不知道如何解决这个问题。请问有什么建议吗?

python pandas pivot
1个回答
0
投票

好吧把它放在这里,因为它不适合在评论中,使用上下文管理器怎么样:

RA = pd.read_excel(file_path, header=0, sheet_name="Data")
pivot_df = pd.DataFrame(RA)
#Inserting Pivot
pivot = pivot_df[filter2].pivot_table(index=['a','b', 'c'], values=['Total1','Total2'], aggfunc='sum', margins=True, margins_name='Total')

#copy & pasting pivot to Summary sheet
with pd.ExcelWriter(file_path, engine='openpyxl', mode='a', if_sheet_exists='overlay') as writer:
    pivot.to_excel(writer, sheet_name='Summary', index=True)
© www.soinside.com 2019 - 2024. All rights reserved.