to_csv附加模式不附加到下一个新行 [英] to_csv append mode is not appending to next new line

查看:2161
本文介绍了to_csv附加模式不附加到下一个新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 test.csv 的csv如下所示:

 准确度阈值trainingLabels 
abc 0.506 15000
eew 18.12 15000

一个名为 summaryDF 的数据框,如下所示:

 准确度阈值trainingLabels 
def 0.116 342
dja 0.121 1271

我在做:

  try:
if os.stat('test.csv')。st_size> 0:
summaryDF.to_csv(path_or_buf = f,encoding ='utf-8',mode ='a',header = False)
f.close()
else:
打印空文件
with open('test.csv','w +')as f:
summaryDF.to_csv(path_or_buf = f,encoding ='utf-8')
f.close()
除了OSError:
printNo file
with open('test.csv','w +')as f:
summaryDF.to_csv(path_or_buf = f,encoding ='utf-8')
f.close()

我希望我的文件是:

 准确度阈值trainingLabels 
abc 0.506 15000
eew 18.12 15000 $ b相反,它是:



 准确度阈值trainingLabels 
abc 0.506 15000
eew 18.12 15000def 0.116 342
dja 0.121 1271

我如何解决这个问题?我想使用CSV作者,而不是 to_csv ,但显然append模式不跳过现有文件的最后一行。

解决方案

您使用的是pandas包吗?你不会在任何地方提及。



Pandas不会自动附加新行,我不知道如何强制它。但你可以这样做:

  f.write('\\\
')
summaryDF.to_csv(path_or_buf = f,mode ='a',...)






您的代码中一个无关的错误:



您似乎有一个名为 f 的全局文件对象。 p>

当你这样做:

  ,'w +')as f:
...
f.close()

您正在关闭的文件是您刚刚在块中打开的文件。您不是关闭全局文件 f ,因为变量被该范围中的 f 遮蔽。



这是你想要的吗?无论哪种方式,这都没有意义。我们使用和范围的原因是为了避免显式关闭文件。



您可以使用:

  f = open('filename')
...
f.close $ b

  with open('filename')as f:
...

不关闭在块中打开的文件。使用块具有额外的优点,即使出现异常并且未执行以下代码,文件也会被关闭。


I have a csv called test.csv that looks like:

accuracy    threshold   trainingLabels
abc         0.506       15000
eew         18.12       15000

And then a dataframe called summaryDF that looks like:

accuracy    threshold   trainingLabels
def         0.116       342
dja         0.121       1271

I am doing:

try:
        if os.stat('test.csv').st_size > 0:
                summaryDF.to_csv(path_or_buf=f, encoding='utf-8', mode='a', header=False)
                f.close()
        else:
            print "empty file"
            with open('test.csv', 'w+') as f:
                summaryDF.to_csv(path_or_buf=f, encoding='utf-8')
                f.close()
    except OSError:
        print "No file"
        with open('test.csv', 'w+') as f:
            summaryDF.to_csv(path_or_buf=f, encoding='utf-8')
            f.close()

Because I want my file to be:

accuracy    threshold   trainingLabels
abc         0.506       15000
eew         18.12       15000
def         0.116       342
dja         0.121       1271

Instead, it is:

accuracy    threshold   trainingLabels
abc         0.506       15000
eew         18.12       15000def            0.116       342   
dja         0.121       1271

How can I solve this? I am guessing using a CSV writer instead of to_csv but clearly the append mode is not skipping the last line of the existing file.

解决方案

Are you using the pandas package? You do not mention that anywhere.

Pandas does not automatically append a new line, and I am not sure how to force it. But you can just do:

f.write('\n')
summaryDF.to_csv(path_or_buf=f, mode='a', ...)


An unrelated bug in your code:

You seem to have a global file object called f.

When you do this:

with open('test.csv', 'w+') as f:
    ...
    f.close()

The file that you are closing there is the file that you just opened in the with block. You are not closing the global file f because the variable was overshadowed by the f in that scope.

Is this what you want? Either way, it makes no sense. The reason why we use the with scope is to avoid having to close the file explicitly.

You either use:

f = open('filename')
...
f.close()

OR

with open('filename') as f:
    ...

You do not close a file opened within a with block. Using a with block has the additional advantage that the file gets closed even if an exception is raised and the following code is not executed.

这篇关于to_csv附加模式不附加到下一个新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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