Skip to content
Advertisement

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

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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement