pavan sunder
pavan sunder

Reputation: 109

How to write a python list to a specific row in a csv file?

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]) 

Upvotes: 1

Views: 1952

Answers (2)

mssnrg
mssnrg

Reputation: 96

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

Use:

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

Upvotes: 1

lostbard
lostbard

Reputation: 5230

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.

Upvotes: 0

Related Questions