[Python] Adding multiple empty rows to a csv file

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have a csv file with around 500 lines, i want to insert multiple empty rows for each row , i.e add 5 empty rows after each row, so i tried this

    import csv
    with open("new.csv", 'r') as infile:
    read=csv.reader(infile, delimiter=',')
    with open("output1.csv", 'wt') as output:
    outwriter=csv.writer(output, delimiter=',')
    i = 0
    for row in read:
    outwriter.writerow(row)
    i += 1
    outwriter.writerow([])


    This creates 3 empty rows but not 5, i am not sure on how to add 5 rows for each row. what am i missing here

    Update: CSV File sample

    No,First,Sec,Thir,Fourth
    1,A,B,C,D
    2,A,B,C,D
    3,A,B,C,D
    4,A,B,C,D
    5,A,B,C,D
    6,A,B,C,D
    7,A,B,C,D
    8,A,B,C,D


    Adding the output csv file for answer code [​IMG]

    Continue reading...

Compartilhe esta Página