yoann
yoann

Reputation: 311

csv.writer.writerows missing rows from list

I'm new to python.

I have a list with 19188 rows that I want to save as a csv.

When I write the list's rows to the csv, it does not have the last rows (it stops at 19112).

Do you have any idea what might cause this?

Here is how I write to the csv:

mycsvfile = open('file.csv', 'w')
thedatawriter = csv.writer(mycsvfile, lineterminator = '\n')
list = []
#list creation code
thedatawriter.writerows(list)

Each row of list has 4 string elements.

Another piece of information:

If I create a list that contains only the last elements that are missing and add them to the csv file, it kind of works (it is added, but twice...).

mycsvfile = open('file.csv', 'w')
thedatawriter = csv.writer(mycsvfile, lineterminator = '\n')
list = []
#list creation code
thedatawriter.writerows(list)
list_end = []
#list_end creation code
thedatawriter.writerows(list_end)

If I try to add the list_end alone, it doesn't seem to be working. I'm thinking there might be a csv writing parameter that I got wrong.

Another piece of information:

If I open the file adding ", newline=''", then it write more rows to it (though not all)

mycsvfile = open('file.csv', 'w', newline='')

There must be a simple mistake in the way I open or write to the csv (or in the dialect?)

Thanks for your help!

Upvotes: 2

Views: 3001

Answers (1)

yoann
yoann

Reputation: 311

I found my answer! I was not closing the filehandle before script end which left unwritten rows.

Here is the fix:

with open('file.csv', 'w', newline='') as mycsvfile:
    thedatawriter = csv.writer(mycsvfile, lineterminator = '\n')
    thedatawriter.writerows(list)  

See: Writing to CSV from list, write.row seems to stop in a strange place

Close the filehandle before the script ends. Closing the filehandle will also flush any strings waiting to be written. If you don't flush and the script ends, some output may never get written.

Using the with open(...) as f syntax is useful because it will close the file for you when Python leaves the with-suite. With with, you'll never omit closing a file again.

Upvotes: 4

Related Questions