Search code examples
pythonloopscsvtextrows

How to write data onto csv file over a loop in python


I want to write data onto several rows in a csv file. With this code I am only getting a single line written onto file. Need help.

for i in range(1,10):
    with open("output.csv",'wb') as f:
        writer = csv.writer(f, dialect='excel')
        writer.writerow([value1, value2, value3])

Solution

  • You are encountering the error because it is re-writing a csv for every iteration in your loop. You should move the with open() statement outside of your loop block.