简体   繁体   English

如何在使用 python pandas .to_csv 附加到 csv 时强制换行

[英]How to force a new line when appending to a csv using python pandas .to_csv

When appending to csv, my first line is starting on the existing last line rather than a new line.附加到 csv 时,我的第一行从现有的最后一行而不是新行开始。

I keep searching SO, but I am just finding the basic use of opening a csv in append mode or using append mode when writing to csv.我一直在搜索 SO,但我只是找到了在追加模式下打开 csv 或在写入 csv 时使用追加模式的基本用法。 I could not make sense of the accepted answer here ( to_csv append mode is not appending to next new line ) since it appears to require the existing file to be open before writing the ("/n") with f.write("/n") .我无法理解这里接受的答案( to_csv 追加模式不追加到下一个新行),因为它似乎要求在使用f.write("/n") ") 写入 ("/n") 之前打开现有文件f.write("/n") This answer ( How to add pandas data to an existing csv file? ) is most relevant, but I am hoping to write multiple data frames in a function, so I do not want to keep opening them.这个答案( How to add pandas data to an existing csv file? )是最相关的,但我希望在一个函数中写入多个数据框,所以我不想继续打开它们。 My plan is to use a function like:我的计划是使用如下函数:

import os
def mysave(df,dfpath):
    # if file does not exist write header 
    if not os.path.isfile(dfpath):
        df.to_csv(dfpath, index = False)
    else: # else it exists so append without writing the header
        df.to_csv(dfpath, mode = 'a', index = False, header = False)

mysave(mydf, 'foo.csv')

I've created a very simple example, with foo.csv with the structure:我创建了一个非常简单的示例,其中 foo.csv 的结构如下:

a   b   c   d           
5   1   ah  doo         
6   2   bah poo         
7   2   dah coo

When I use my function or this simple code:当我使用我的函数或这个简单的代码时:

import pandas as pd
df = pd.read_csv('foo.csv', index_col=False)
mydf = df
mydf.to_csv('foo.csv', mode='a', index = False, header = False)

This is what foo.csv ends up as:这就是 foo.csv 最终的结果:

a   b   c   d           
5   1   ah  doo         
6   2   bah poo         
7   2   dah coo5    1   ah  doo
6   2   bah poo         
7   2   dah coo     

When I attempt to add a carriage return character as the header, like mydf.to_csv('foo.csv', mode='a', index = False, header = ("/n")) pandas (rightly) ignores my erroneous header comment and goes with the default of header = True .当我尝试添加回车符作为标题时,例如mydf.to_csv('foo.csv', mode='a', index = False, header = ("/n")) pandas(正确地)忽略了我的错误header 注释并使用header = True的默认值。

a   b   c   d           
5   1   ah  doo         
6   2   bah poo         
7   2   dah cooa    b   c   d
6   2   bah poo         
7   2   dah coo 

I had a similar problem and after a god bit of searching, I didn't find any simple/elegant solution.我有一个类似的问题,经过一番搜索,我没有找到任何简单/优雅的解决方案。 The minimal fix that worked for me is:对我有用的最小修复是:

import pandas as pd

with open('foo.csv') as f:
    f.write('\n')    
mydf.to_csv('foo.csv', index = False, header = False, mode='a')

I am assuming that you are going to appending one below other of two dataframe into single dataframe.我假设您要将两个数据帧中的另一个附加到单个数据帧中。

use below mentioned command to make it as single command使用下面提到的命令将其作为单个命令

ans = pd.concat([df, df])

then you can make output into .csv file然后您可以将输出转换为 .csv 文件

If your dataframe gets huge and you want to avoid concatenation you could go with如果您的数据框变得很大并且您想避免串联,则可以使用

import csv
with open('foo.csv','ab') as out:
   writer=csv.writer(out)
   writer.writerow(())

in a function or just as a snippet in your code.在函数中或作为代码中的片段。 If you're not on Windows maybe you could avoid adding 'b' in open and open the file with just 'a' (append)如果您不在 Windows 上,也许您可​​以避免在 open 中添加 'b' 并仅使用 'a' (追加)打开文件

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

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