使用to_csv()后关闭文件 [英] Closing file after using to_csv()

查看:207
本文介绍了使用to_csv()后关闭文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,到目前为止,我爱ipython笔记本进行学习.我是否使用to_csv()函数将pandas数据帧写出到文件中.我想打开csv来查看它在excel中的外观,它只能以只读模式打开,因为它仍在被另一个用户使用.我如何关闭文件?

I am new to python and so far I am loving the ipython notebook for learning. Am I using the to_csv() function to write out a pandas dataframe out to a file. I wanted to open the csv to see how it would look in excel and it would only open in read only mode because it was still in use by another How do I close the file?

import pandas as pd
import numpy as np
import statsmodels.api as sm
import csv

df = pd.DataFrame(file)
path = "File_location"

df.to_csv(path+'filename.csv', mode='wb')

这将写出文件没有问题,但是当我在excel中检查"它时,我得到了只读警告.这也给我提出了一个更大的问题.有没有办法查看python当前正在使用/触摸的文件?

This will write out the file no problem but when I "check" it in excel I get the read only warning. This also brought up a larger question for me. Is there a way to see what files python is currently using/touching?

推荐答案

@rpattiso 谢谢.

@rpattiso thank you.

尝试自己打开和关闭文件:

try opening and closing the file yourself:

outfile = open(path+'filename.csv', 'wb')
df.to_csv(outfile)
outfile.close()     

这篇关于使用to_csv()后关闭文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆