0

At the moment, I know how to write a list to one row in csv, but when there're multiple rows, they are still written in one row. What I would like to do is write the first list in the first row of csv, and the second list to the second row.

Code:

for i in range(10):
    final=[i*1, i*2, i*3]
    with open ('0514test.csv', 'a') as file:
        file.write(','.join(str(i) for i in kk))
1
  • You should have a look at csv package Commented May 14, 2018 at 7:10

2 Answers 2

1

You may want to add linebreak to every ending of row. You can do so by typing for example:

file.write('\n')
Sign up to request clarification or add additional context in comments.

Comments

0

The csv module from standard library provides objects to read and write CSV files. In your case, you could do:

import csv
for i in range(10):
    final = [i*1, i*2, i*3]
    with open("0514test.csv", "a", newline="") as file:
        writer = csv.writer(file)
        writer.writerow(final)

Using this module is often safer in real life situations because it takes care of all the CSV machinery (adding delimiters like " or ', managing the cases in which your separator is also present in your data etc.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.