423,319 Members | 2,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,319 IT Pros & Developers. It's quick & easy.

Problem with writing 2 arrays to CSV file in 2 columns

P: 1
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.
Nov 13 '17 #1

✓ answered by hpmachining

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.
Expand|Select|Wrap|Line Numbers
  1. columnA = ["aa", "bb", "cc", "dd", "ee"]
  2. columnB = [12, 23, 34, 45, 56]
  3.  
  4. for a, b in zip(columnA, columnB):
  5.     print('{}, {}'.format(a, b))
  6.  

Share this Question
Share on Google+
1 Reply


P: 15
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.
Expand|Select|Wrap|Line Numbers
  1. columnA = ["aa", "bb", "cc", "dd", "ee"]
  2. columnB = [12, 23, 34, 45, 56]
  3.  
  4. for a, b in zip(columnA, columnB):
  5.     print('{}, {}'.format(a, b))
  6.  
Nov 20 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.