简体   繁体   English

尝试将数据框写入 .CSV - 权限被拒绝

[英]Trying to write a data frame to .CSV - Permission denied

I am trying to write a datafame to a csv file using the following:我正在尝试使用以下方法将 datafame 写入 csv 文件:

fxRollPath = 'C:\Users\stacey\Documents\scripts\FXFiles'
fxData.to_csv(fxRollPath,fxRoll'+ str(index)+'.csv')

Where fxRoll is a dataframe其中 fxRoll 是一个数据框

But I get the error:但我收到错误:

PermissionError: [Errno 13] Permission denied: 'C:\Users\stacey\Documents\scripts\FXFiles'

What have I done wrong?我做错了什么?

Just realized, you are actually trying to save to a target directory path instead of file path.刚刚意识到,您实际上是在尝试保存到目标目录路径而不是文件路径。

Docs of path_or_buf for DataFrame.to_csv : "string or file handle, default None. File path or object, if None is provided the result is returned as a string." path_or_bufDataFrame.to_csv文档:“字符串或文件句柄,默认为无。文件路径或对象,如果没有提供,则结果作为字符串返回。”

So change your code to:因此,将您的代码更改为:

fxData.to_csv('{0}\{1}{2}{3}'.format(fxRollPath, fxRoll, str(index), '.csv'))

也许您需要关闭现在打开的 csv 的第一个版本

I got this problem too.我也遇到了这个问题。 Not sure if you have the same situation as me.不知道你有没有和我一样的情况。

I had the same filename in the same directory and I wanted to override the old csv file.我在同一目录中有相同的文件名,我想覆盖旧的 csv 文件。 Instead of overriding the old file, I deleted it and then save it to solve this problem.我没有覆盖旧文件,而是将其删除然后保存以解决此问题。

        os.remove('filename') 
        df.to_csv('filename.csv')
        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM