1

I'm sure there is an easy way to do this, so here goes. I'm trying to export my lists into CSV in columns. (Basically, it's how another program will be able to use the data I've generated.) I have the group called [frames] which contains [frame001], [frame002], [frame003], etc. I would like the CSV file that's generated to have all the values for [frame001] in the first column, [frame002] in the second column, and so on. I thought if I could save the file as CSV I could manipulate it in Excel, however, I figure there is a solution that I can program to skip that step.

This is the code that I have tried using so far:

import csv

data = [frames]
out = csv.writer(open(filename,"w"), delimiter=',',quoting=csv.QUOTE_ALL)
out.writerow(data)

I have also tried:

import csv

myfile = open(..., 'wb')
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(mylist)

If there's a way to do this so that all the values are space separated, that would be ideal, but at this point I've been trying this for hours and can't get my head around the right solution.

Steve Edwards
  • 113
  • 1
  • 5
  • 14
  • possible duplicate of [Exporting a list to a CSV or XML and each sublist in its own column - Repost](http://stackoverflow.com/questions/11620597/exporting-a-list-to-a-csv-or-xml-and-each-sublist-in-its-own-column-repost) –  Jul 24 '12 at 09:32

1 Answers1

1

What you're describing is that you want to translate a 2 dimensional array of data. In Python you can achieve this easily with the zip function as long as the inner lists are all the same length.

out.writerows(zip(*data))

If they are not all the same length, you can use itertools.izip_longest to fill the remaining fields with some default value (even '').

kojiro
  • 74,557
  • 19
  • 143
  • 201
  • You'll have to excuse me as I am new to Python. I could not get that code to work. This is what I have so far: import csv data = [frames] out = csv.writer(open(filename,"w"), delimiter=',',quoting=csv.QUOTE_ALL) out.writerows(zip(*data)) – Steve Edwards Jul 23 '12 at 20:05