1

I have a csv file which has 2 rows of data. I have a python list to be written to the next immediate row in the csv file ie row number 3 without altering the contents of the first two rows.

I wrote a code but it is always writing to the first row. Below is my code. Kindly suggest where to specify the row number in the code.

import sys,csv    
with open("C:\pavan\pav.csv",'w',newline='') as wr:
    lst=[9,10,11,12]
    writer = csv.writer(wr, delimiter = ',')
    writer.writerows([lst]) 
pavan sunder
  • 99
  • 4
  • 15
  • 1
    Possible duplicate of [append new row to old csv file python](https://stackoverflow.com/questions/2363731/append-new-row-to-old-csv-file-python) – thegreenogre May 24 '17 at 22:34

2 Answers2

1

Just open the csv file in append mode. This will solve your problem.

Use:

with open("pav.csv",'a',newline='') as wr:
BSMP
  • 4,596
  • 8
  • 33
  • 44
mssnrg
  • 96
  • 3
0

You need to open the file in append mode so that it will write to the end of the file:

with open("C:\pavan\pav.csv",'a',newline='') as wr:

This will open the file in write mode, and append to the end of the file if it exists.

lostbard
  • 5,065
  • 1
  • 15
  • 17