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.