I have a dataframe and I want to save it to a csv file. This operation is quite simple because I just need to use the following command:
df.to_csv(namefile, sep='', index=False)
The output is a csv file where each line contains the content of the a row of the dataframe. The output is this:
A,B,C,D
1,2,3,4
5,6,7,8
9,1,2,3
However, what I would like to do is to have a blank line every other row so that the output looks like this:
A,B,C,D
1,2,3,4
5,6,7,8
9,1,2,3
Basically I need to add the CR and LF symbol between every other line. Can you suggest me a smart and elegant way to achieve my goal?
Use parameter line_terminator='\n\n'
:
line_terminator : string, default '\n'
The newline character or character sequence to use in the output file
Demo:
df.to_csv(namefile, line_terminator='\n\n')
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments