python pandas to excel错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/18758104/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python pandas to excel error
提问by jonas
I have a dataframe that I want to export to Excel. I'm new to python and pandas so I need some help on this simple task.
我有一个要导出到 Excel 的数据框。我是 python 和 pandas 的新手,所以我需要一些帮助来完成这个简单的任务。
df2.to_excel('C:\BT\stack_test3.xlsx')
Error message:
错误信息:
IOError: [Errno 13] Permission denied: 'C:\BT\stack_test3.xlsx'
IOError: [Errno 13] 权限被拒绝: 'C:\BT\stack_test3.xlsx'
回答by EdChum
You path is incorrect, because you have not escaped the slashes it thinks you are trying to write to the root of c: drive use the following:
您的路径不正确,因为您没有转义它认为您正在尝试写入 c: 驱动器根目录的斜杠,请使用以下命令:
df2.to_excel(r'C:\BT\stack_test3.xlsx')
The rmakes the path a raw string and means you do not need to escape the slashes
将r使得路径的原始字符串,你不需要逃避斜线手段
Edit
编辑
It seems that there is some error with openpyxlas using 
使用openpyxlas似乎有一些错误
df2.to_excel(r'C:\BT\stack_test3.xls')
works which uses xlwt, I don't know enough about those packages so it could be either a permissions problem with openpyxlwhich I have not been able to find anything about or a bug.
使用 的作品xlwt,我对这些包的了解不够,所以这可能是权限问题openpyxl,我无法找到任何相关信息或错误。
回答by OParker
I had an identical problem. Turns out it's because I had left the Excel file open whilst I was trying to write to it. Apparently it doesn't like that. If you have it open try closing it.
我有一个相同的问题。原来这是因为我在尝试写入 Excel 文件时让 Excel 文件处于打开状态。显然它不喜欢那样。如果您打开它,请尝试关闭它。
回答by M_TRONIC
To confirm...in case future readers stumble in this page...before complicating things make sure that the excel file you are trying to save is not already open or to be safe.
确认...以防将来的读者在此页面上绊倒...在使事情复杂化之前,请确保您尝试保存的 excel 文件尚未打开或安全。
Just close all of excel and try save it again.
只需关闭所有 excel 并再次尝试保存。
That should do it.
那应该这样做。
回答by Zagfai
You should write to another drive like 'D:' because in Windows Vista or above that you have no permission to write to 'C:\' and you have no reason to earn the permission.
您应该写入另一个驱动器,例如“D:”,因为在 Windows Vista 或更高版本中,您没有写入“C:\”的权限,也没有理由获得该权限。
回答by Gul Saeed khattak
After closing all instances of excel and running python code works.
关闭所有 excel 实例并运行 python 代码后。

