0

I have 3 lists which contain different data:

list_1 = [1, 2, 3, 4, 5]
list_2 = [0.1, 0.9, 0.6, 0.8, 0.9]
list_3 = [11, 23, 13, 14, 25]

How to output to 3 different columns in CSV (such as list_1 to column 1, list_2 to column 2, etc.)?

column_1  column_2   column_3
1         0.1        11
2         0.9        23
3         0.6        13
4         0.8        14
5         0.9        25

5 Answers 5

1

On doing zip() over the three list, you will get the resultant list as:

>>> zip(list_1, list_2, list_3)
[(1, 0.1, 11), (2, 0.9, 23), (3, 0.6, 13), (4, 0.8, 14), (5, 0.9, 25)]

Now you may write entire nested list to the CSV file using csvwriter.writerows as:

import csv

zipped_list = zip(list_1, list_2, list_3)
columns = ['column_1', 'column_2', 'columns_3']

with open("output.csv", "wb") as f:
    writer = csv.writer(f)
    writer.writerow(columns)  # Add column name, single line
    writer.writerows(zipped_list)  # Add nested structure, multi line

Content of output.csv:

column_1,column_2,columns_3
1,0.1,11
2,0.9,23
3,0.6,13
4,0.8,14
5,0.9,25
Sign up to request clarification or add additional context in comments.

Comments

0

use zip as below and csv writer with delimiter \t

with open("output.csv", "wb") as f:
    writer = csv.writer(f, delimiter='\t')
    writer.writerow(('column_1','column_2','column_3'))
    for i in zip(list_1,list_2,list_3):
        writer.writerow(i)

# column_1  column_2   column_3
# 1       0.1     11
# 2       0.9     23
# 3       0.6     13
# 4       0.8     14
# 5       0.9     25

Comments

0
import csv
list_1=[1, 2, 3, 4, 5]
list_2=[0.1,0.9,0.6,0.8,0.9]
list_3=[11,23,13,14,25]
with open('you_data.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(('column_1','column_2' ,'column_3'))
    writer.writerows(zip(list_1, list_2, list_3))

out:

column_1,column_2,column_3
1,0.1,11
2,0.9,23
3,0.6,13
4,0.8,14
5,0.9,25

to make sure i did not copy from anyone, why should i copy such simple code???

2 Comments

It doesn't, but who knows. Your original answer was based on the usage of writer.row() which you later changed to writer.rows()
i now csv has this method, i want to try it, and i want to make my code clean.
0

You can use this (python2):

import csv
import sys

list_1 = [1, 2, 3, 4, 5]
list_2 = [0.1,0.9,0.6,0.8,0.9]
list_3 = [11,23,13,14,25]

with open('output.csv', 'wb') as f:
    w = csv.writer(f)
    for item  in zip(list_1, list_2, list_3):
        w.writerow(item)

Comments

0

Another simple way to do it (using "zip()" built-in method):

list1 = [1,2,3,4,5]
list2 = [6,7,8,9,10]
list3 = [11,12,13,14,15]
zippedLists = zip(list1, list2, list3)

print("Column_1\tColumn_2\tColumn_3")
for x,y,z in zippedLists:
    print(str(x) + "\t\t" + str(y) + "\t\t" + str(z))

Output is:

Column_1        Column_2        Column_3
1               6               11
2               7               12
3               8               13
4               9               14
5               10              15
Press any key to continue . . .

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.