A much safer way to do this, is to open a file for input,
read data until the place where you want to insert a new row, writing each row to the new file,
then insert new row,
continue reading old file, writing all rows to new file.
After you inspect the results and find they are OK, you can replace the old file with the new one.
I strongly advise keeping a backup of the original file first.
example read/write loop:
read data until the place where you want to insert a new row, writing each row to the new file,
then insert new row,
continue reading old file, writing all rows to new file.
After you inspect the results and find they are OK, you can replace the old file with the new one.
I strongly advise keeping a backup of the original file first.
example read/write loop:
with open('test.csv', 'r') as f_in, open('testNew.csv', 'w') as f_out: reader = csv.reader(f_in, delimiter=',') # modify for your file writer = csv.writer(f_out, delimiter=',') # modify for your file for row in reader: writer.writerow(row) if row == ... # add insert condition writer.writerow(webdata)Of course above code won't run until you add condition information.