Problem with writing 2 arrays to CSV file in 2 columns

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sabres909
    New Member
    • Nov 2017
    • 1

    Problem with writing 2 arrays to CSV file in 2 columns

    Hi all

    I am new to Python and need to write two lists to a CSV file, in 2 columns:

    For example:

    columnA=["aa","bb","cc", "dd","ee"]
    columnB=[12,23,34,45,56]

    I am going round in circles trying different variations and the answer is probably very simple, but could really use some help here.
  • hpmachining
    New Member
    • Oct 2015
    • 15

    #2
    If both columns are the same length, zip will work well. If columns are different lengths, the output will stop when the end of the shorter column is reached. Here is an example that prints to the screen.
    Code:
    columnA = ["aa", "bb", "cc", "dd", "ee"]
    columnB = [12, 23, 34, 45, 56]
    
    for a, b in zip(columnA, columnB):
        print('{}, {}'.format(a, b))

    Comment

    Working...