1

I'm using Python 3.6. I have three lists with about 11,000 elements that I am converting to a csv file, but because there is no writecolumn() function like there is a writerow(), my lists are displayed near infinitely rightward. Here is a sample code with sample lists of only three elements each. How do I get the elements from the lists 'number', 'session', and 'stage' under their own column and header? To clarify, I want all three 'number' elements under one column, and the same for 'session' and 'stage'.

The question here comes close to answering but not quite, so please don't duplicate: Write multiple lists into csv. file in Python

I've tried converting to a dictionary but dictionaries don't take variables in the values section. I've tried iterating but that only makes each element iterable going rightward. I've tried using the zip() function but that only groups each list by index under one column.

import csv
import sys

number = ['H. R. 43', 'H. Con. Res. 22', 'S. 11']
session = ['1st Session', '1st Session', '2nd Session']
stage = ['Introduced in House', 'Engrossed in House', 'Referred in Senate']


def main(anything):
    global number
    global session
    global stage
    with open('BILLS.csv', 'w') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['Number', 'Session', 'Stage'])
        writer.writerow(number)
        writer.writerow(session)
        writer.writerow(stage)


if __name__ == '__main__':
    main(sys.argv)
1
  • I think zip() is what you need here, how have you tried to use it ? Commented Mar 23, 2018 at 15:47

3 Answers 3

0

You can use the zip function

import csv
number = ['H. R. 43', 'H. Con. Res. 22', 'S. 11']
session = ['1st Session', '1st Session', '2nd Session']
stage = ['Introduced in House', 'Engrossed in House', 'Referred in Senate']

# setting newline to '' is required because csv will add blank line 
with open('BILLS.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['Number', 'Session', 'Stage'])
    writer.writerows(zip(number, session, stage))

or you could use the pandas module

import pandas as pd 

number = ['H. R. 43', 'H. Con. Res. 22', 'S. 11']
session = ['1st Session', '1st Session', '2nd Session']
stage = ['Introduced in House', 'Engrossed in House', 'Referred in Senate']

#building a dictionary out of your lists
data = {'number':number, 'session':session, 'stage':stage}

df = pd.DataFrame.from_dict(data)

#pandas even has a buildin to_csv() function
#with open('BILLS.csv', 'w') as f:
#   df.to_csv(f)

You should also stop using global variables

Sign up to request clarification or add additional context in comments.

1 Comment

This worked well, thank you. I also took out the global declarations and input the variables as parameters, instead. For those reading this who don't know, that not only shortens your script but reduces the memory usage per build.
0
from itertools import zip_longest

combine_three_list = zip_longest(number, session, stage)

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
0

Since you have same number of elements in each list you can use zip.

import csv
import sys

number = ['H. R. 43', 'H. Con. Res. 22', 'S. 11']
session = ['1st Session', '1st Session', '2nd Session']
stage = ['Introduced in House', 'Engrossed in House', 'Referred in Senate']


def main():
    global number
    global session
    global stage
    with open('BILLS.csv', 'w') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['Number', 'Session', 'Stage'])
        for ele in list(zip(number, session, stage)):
            writer.writerow(ele)


if __name__ == '__main__':
    main()

Comments