Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert alternate blank lines into a table with Python?

I have a simple (but huge) CSV table, and I need to insert a free (=blank) line/row after each line/row of this table. To explain it in another way, I want every second line of my table to be blank (but without deleting/overwriting any of the original lines). I have tried a lot of ways, but this is the best I could come up with:

with open(sys.argv[1], 'r') as input:
   readie=csv.reader(input, delimiter=',')
   with open("output.csv", 'wt', newline='') as output:
       outwriter=csv.writer(output, delimiter=',')
       for row in readie:
           row_plus = (row, \n)
           outwriter.writerow(row_plus)

It doesn't seem to work because it crams all the columns in the table into one column and interprets (row, \n) as two columns only. It also just prints "\n" and doesn't recognize that I want it to insert another line break.

like image 878
Frank Avatar asked May 02 '26 09:05

Frank


1 Answers

How about:

  for row in readie:
     outwriter.writerow(row)
     outwriter.writerow([])

Seems to do what you want.

like image 157
larsks Avatar answered May 04 '26 23:05

larsks