DataFrame写入excel时,Explicit close() may be required for workbook?
因为平时做了很多excel要分到不同的人手中,就用pandas做了一个excel拆分的文件。
报错相关的代码如下:
for i in city_set:
print(i)
if os.path.exists(to_filepath+to_filename+'_%s.xlsx' % i ) == True:
print('%s同名文件已存在' % i)
continue
print(i,'拆分中')
writer = pd.ExcelWriter(to_filepath+to_filename+'_%s.xlsx' % i)
for j in data.keys():
data_tmp = data[j][data[j].iloc[:,0]==i]
if len(data_tmp.index)==0:
data_tmp=pd.DataFrame({j:[i,"没有数据"]})
data_tmp.to_excel(writer,sheet_name = j)
开始用的spyder,只是会缺失文件。
pycharm提示的错误码为:
Exception ignored in: <bound method Workbook.__del__ of <xlsxwriter.workbook.Workbook object at 0x000001D5FCCEDA58>>
Traceback (most recent call last):
File "C:\Program Files\Anaconda3\lib\site-packages\xlsxwriter\workbook.py", line 149, in __del__
raise Exception("Exception caught in workbook destructor. "
Exception: Exception caught in workbook destructor. Explicit close() may be required for workbook.
这是pandas的源码:
def __del__(self):
"""Close file in destructor if it hasn't been closed explicitly."""
try:
if not self.fileclosed:
self.close()
except:
raise Exception("Exception caught in workbook destructor. "
"Explicit close() may be required for workbook.")
请大神指教。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加writer.save()