问题描述
我正在使用 jupyter notebook pandas to_csv
不会将数据帧输出到文件.
I'm using jupyter notebook pandas to_csv
doesn't output the dataframe to a file.
我尝试使用 to_csv
通过设置工作目录或指定目录将数据帧输出到 csv 文件,但它没有创建任何文件.代码运行并没有产生任何错误信息,但是当我打开文件夹时,没有这样的文件.
I tried to use to_csv
to output a dataframe to a csv file by setting the working directory or specify the directory, and it didn't create any file. The code ran and didn't produce any error message, but when I opened the folder, there was no such file.
我尝试了不同的 IO,它确实显示结果已输出.
I tried a different IO, and it did show the result had been output.
from io import StringIO
output = StringIO()
a.to_csv(output)
print(output.getvalue())
我得到以下输出:
,a
0,1
1,2
2,3
但同样 to_csv('filepath/filename.csv')
不输出任何文件.
but again to_csv('filepath/filename.csv')
doesn't output any file.
PS:我可以使用 read_csv() 读取任何目录中的任何文件.
PS: I can read any file in any directory using read_csv().
如果我保存文件 df.to_csv('testfile.csv')
然后做 pd.read_csv('testfile.csv')
If I save the file df.to_csv('testfile.csv')
then do pd.read_csv('testfile.csv')
我可以读取文件,但在目录中看不到它.
I can read the file but cannot see it in the directory.
此外,执行 [x for x in os.listdir() if x == 'testfile.csv']
将列出文件.
Also, doing [x for x in os.listdir() if x == 'testfile.csv']
will list the file.
推荐答案
我在使用 spyder 时遇到了同样的问题.就我而言,它是由我使用的互联网安全工具 (COMODO) 引起的,它以某种方式在沙箱中执行了 spyder,并且不允许它写入正常"目录.相反,它将结果保存到名为 C:VTRoot\HarddiskVolume2\users\ 的文件夹中.您可以尝试使用非常独特的名称 a.to_csv('very_unique_filename.csv') 保存文件,然后在 Windows 资源管理器中搜索该文件名以找到存储它的文件夹.如果原因是这样的工具,更改其设置可能会有所帮助.
I had the same problem using spyder. In my case it was caused by the internet security tool (COMODO) I used, which somehow executed spyder in a sandbox or so and not allowed it to write to the "normal" directories. Instead it saved the result to a folder named C:VTRoot\HarddiskVolume2\users\. You can try to save a file with a quite unique name a.to_csv('very_unique_filename.csv') and then search in the windows explorer for that filename to find the folder it is stored in. If the reason is some tool like this, changing it's settings may help.
这篇关于pandas to_csv 不输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!