将 Pandas 数据帧写入 xlsx 文件时出现权限错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45479110/
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
Permission error when pandas dataframe is write to xlsx file
提问by Anand Vamsee
I get this error while i want to keep my dataframe in excel file which name pandas_simple.xlsx
我想将数据框保存在名为 pandas_simple.xlsx 的 excel 文件中时出现此错误
Below is my error:
以下是我的错误:
This is my code:
这是我的代码:
import pandas as pd
df = pd.DataFrame({'Car': [101, 20, 350, 20, 15, 320, 454]})
writer = pd.ExcelWriter('pandas_simple.xlsx')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
writer.close()
Anyone can share some idea to me here?
任何人都可以在这里与我分享一些想法吗?
回答by Michail N
You try to write to a folder where you need administration rights. Change:
您尝试写入需要管理权限的文件夹。改变:
writer = pd.ExcelWriter("pandas_simple.xlsx")
to:
到:
writer = pd.ExcelWriter("C:\...\pandas_simple.xlsx")
with the full path and you will not have a problem.
使用完整路径,你不会有问题。
回答by Anand Vamsee
This error could also occur if you have a version of the same file (pandas_simple.xlsx in this case) already open in your desktop. In that case, python will not have permission to close and overwrite the same file. Closing the excel file and re-running the script should resolve the issue.
如果您的桌面上已经打开了相同文件(在本例中为 pandas_simple.xlsx)的版本,也可能发生此错误。在这种情况下,python 将无权关闭和覆盖同一个文件。关闭 excel 文件并重新运行脚本应该可以解决问题。
回答by f1nan
The documentation of pandas.DataFrame.to_excelsays that the first argument can be a string that represents the file path. In your case i would drop all lines with writer and just try
pandas.DataFrame.to_excel的文档说第一个参数可以是表示文件路径的字符串。在你的情况下,我会放弃与作家的所有线条,然后尝试
df.to_excel('pandas_simple.xlsx')
That should write pandas_simple.xlsx to your current working directory. If that does not work try to provide the full path name (e.g. C:\\Users\\John\\pandas_simple.xlsx). Also make sure that you don't try to write to a directory which needs adminstration rights.
这应该将 pandas_simple.xlsx 写入您当前的工作目录。如果这不起作用,请尝试提供完整路径名(例如 C:\\Users\\John\\pandas_simple.xlsx)。还要确保您不要尝试写入需要管理权限的目录。
回答by Aladahalli S Bakkesh
What if the path is correct?!!!
如果路径正确呢?!!!
Try closing the xlsx file opened in Excel application and run the code again, it worked for me and same should happen with you.
尝试关闭在 Excel 应用程序中打开的 xlsx 文件并再次运行代码,它对我有用,你也会发生同样的情况。
I am attaching my code snippet for your reference
我附上我的代码片段供您参考
import pandas as pd
file='C:/Users/Aladahalli/Desktop/Book1.xlsx'
xls = pd.ExcelFile(file)
df = pd.read_excel(xls, sheet_name='Sheet1')
#create a column by name Final and store concatinated columns
df["Final"] = df["Name"] + " " + df["Rank/Designation"] + " " + df["PS"]
print(df.head())
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('C:/Users/Aladahalli/Desktop/Final.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
writer.close()