Python Pandas to_csv() 检查覆盖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40375366/
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
Pandas to_csv() checking for overwrite
提问by Robin Kramer
When I am analyzing data, I save my dataframes into a csv-file and use pd.to_csv()
for that. However, the function (over)writes the new file, without checking whether there exists one with the same name. Is there a way to check whether the file already exists, and if so, ask for a new filename?
当我分析数据时,我将数据帧保存到一个 csv 文件中并pd.to_csv()
用于该文件。但是,该函数(覆盖)写入新文件,而不检查是否存在同名文件。有没有办法检查文件是否已经存在,如果存在,要求一个新的文件名?
I know I can add the system's datetime to the filename, which will prevent any overwriting, but I would like to know when I made the mistake.
我知道我可以将系统的日期时间添加到文件名中,这将防止任何覆盖,但我想知道我何时犯了错误。
采纳答案by tda
Try the following:
请尝试以下操作:
import glob
import pandas as pd
# Give the filename you wish to save the file to
filename = 'Your_filename.csv'
# Use this function to search for any files which match your filename
files_present = glob.glob(filename)
# if no matching files, write to csv, if there are matching files, print statement
if not files_present:
pd.to_csv(filename)
else:
print 'WARNING: This file already exists!'
I have not tested this but it has been lifted and compiled from some previous code which I have written. This will simply STOP files overwriting others. N.B. you will have to change the filename variable yourself to then save the file, or use some datetime variable as you suggested. I hope this helps in some way.
我还没有测试过这个,但它是从我以前编写的一些代码中提取和编译的。这将简单地停止文件覆盖其他文件。注意,您必须自己更改文件名变量才能保存文件,或者按照您的建议使用一些日期时间变量。我希望这在某种程度上有所帮助。
回答by Robin Kramer
Based on TaylorDay's suggestion I made some adjustments to the function. With the following code you are asked whether you would like to overwrite an existing file. If not, you are allowed to type in another name. Then, the same write-function is called, which will again check whether the new_filename
exists.
根据 TaylorDay 的建议,我对该功能进行了一些调整。使用以下代码询问您是否要覆盖现有文件。如果没有,您可以输入另一个名称。然后,调用相同的 write-function,它会再次检查是否new_filename
存在。
from os import path
import pandas as pd
def write_csv_df(path, filename, df):
# Give the filename you wish to save the file to
pathfile = os.path.normpath(os.path.join(path,filename))
# Use this function to search for any files which match your filename
files_present = os.path.isfile(pathfile)
# if no matching files, write to csv, if there are matching files, print statement
if not files_present:
df.to_csv(pathfile, sep=';')
else:
overwrite = raw_input("WARNING: " + pathfile + " already exists! Do you want to overwrite <y/n>? \n ")
if overwrite == 'y':
df.to_csv(pathfile, sep=';')
elif overwrite == 'n':
new_filename = raw_input("Type new filename: \n ")
write_csv_df(path,new_filename,df)
else:
print "Not a valid input. Data is NOT saved!\n"