I have a CSV file that was written by a CSV Writer. There is a single empty line at the end of the file that I need to remove. I cannot seem to get rid of it, and my search results are inundated with people asking why there is a blank line between each row of data (which is of course solved by adding newline=""
to the file open statement.
Is there a way to cleanly do this? This is the code:
with open(r"file.csv", "w", newline="") as f:
csv_writer = writer(f, delimiter=";")
for i in range(5):
csv_writer.writerow([i, uniform(0,500)])
And this is the output (actually making the last line empty will not show up in markdown it seems:
0;479.9182863880229
1;397.35174552864135
2;390.33911360875913
3;243.78955947710557
4;171.04998742391697
<empty line here>
I don't like this solution, which is in the same vein as the responses to this question:
lines = file.readlines()
lines = lines[:-1]
Because it requires me to read the entire file again, and I am expecting that this file may grow to an extremely large size, and this particular block of code may end up getting executed many many times over.