473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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.
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.  

1 2307
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

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

Similar topics

2
by: Randy Jackson | last post by:
Hello all. Okay, this seems really stupid, but it's driving me up the wall. I have a simple script I've written to log some information to a text file. Everything seems to be okay, the code...
2
by: Kelly G. | last post by:
Hi all, I have a small problem in writing XML file from VB.net. I am trying to write the XML file using dataset. I am able to write the XML in sequential way(one node after other). I can write...
3
by: giladap | last post by:
I am trying to create a BHO using C#. I currently have it successfully loading into IE (I've checked with "Manage Add-ons"), but nothing else seems to be working. I get no errors that I can see. ...
4
by: VB Programmer | last post by:
I just upgraded my dev system from win 2k to win xp pro. I have a website I'm creating and when I login to the website it writes an entry to my C:\GoodLogin.txt file. It worked fine for win2k. ...
6
by: Richard L Rosenheim | last post by:
I'm trying to write an error log from a web service. I'm getting an exception of "Access to the path {filename} is denied." when I attempt to create the file. I tried specifying the root...
4
by: =?Utf-8?B?QmlsbCBNYW5yaW5n?= | last post by:
I have an internal ASP.NET 2.0 web service that does, among other things, expose a method that causes files to be written to a location that the user configures. The web service has been around...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
3
crystal2005
by: crystal2005 | last post by:
Hi all, Basically, i'm trying to create temporary file using TemporaryFile() module. TemporaryFile( ]]]]) It's done perfectly to create an intended file. But, i encountered a problem to...
12
by: LayneMitch via WebmasterKB.com | last post by:
Hello everyone. I'm currently learning Javascript and doing a few exercises. One problem I'm working on takes an array of names from an xml file using Ajax and writes it to...
0
kirubagari
by: kirubagari | last post by:
void Write_PR_DataFile( char* startDate, char* endDate, char* action, char* lot, char* operation, char* subOperation, char* actualQty, char* equipment, ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.