How can I store a huge list into a csv file?

Question:

For example:

header = ['a','b','c']
data = ['1','2','3','4','5','6',...,'100']

How can I combine each three of the data to match the header?

Expected output:

output

Asked By: An Jiahong

||

Answers:

Here’s how you can do this.

import csv

header = ['a','b','c']
data = ['1','2','3','4','5','6','7','8','9']

file = csv.writer(open(f"testing.csv", "a",newline=''))
file.writerow(header)

for i in range(0,len(data),3):
    new_list = data[i:i+3]
    file.writerow(new_list)


Here’s the screenshot of ‘testing.csv’:

enter image description here

Answered By: Junaid Nazir
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.