How to stop writing a blank line at the end of csv file - pandas
<p>When saving the data to csv, <code>data.to_csv('csv_data', sep=',', encoding='utf-8', header= False, index = False)</code>, it creates a blank line at the end of csv file.  </p>

<p>How do you avoid that?  </p>

<p>It's got to do with the <code>line_terminator</code> and it's default value is <code>n</code>, for new line.</p>

<p>Is there a way to specify the <code>line_terminator</code> to avoid creating a blank line at the end, or do i need to read the csv file, remove the blank line and save it?</p>

<p>Not familiar with pandas.  Your help will be appreciated, thanks in advance!</p>

<p>One way would be to save data except the last entry,with default <code>line_terminator</code>(<code>\n</code>) and append the last line with <code>line_terminator=""</code> .</p>

<pre><code>data1 = data.iloc[0:len(data)-1]
data2 = data.iloc[[len(data)-1]]
data1.to_csv('csv_data', sep=',', encoding='utf-8', header= False, index = False)
data2.to_csv('csv_data', sep=',', encoding='utf-8', header= False, index = False,mode='a',line_terminator="")
</code></pre>

python csv pandas