0

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)

Most recently I have tried:

import csv

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

After more research, I'm actually needing to build an XML file, but I still need the data to be in the correct order. That is, the contents of [frame001] in a column, [frame002] in the next column, etc. All frames contain the same amount of information.

Thanks again for any assistance!

This is an example of the data for what the first 2 columns look like: [255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0], [0, 171, 205, 0, 171, 205, 0, 171, 205, 0, 171, 205, 0, 171, 205, 0, 171, 205, 0, 171, 205, 0, 171, 205, 0, 171, 205, 0, 171, 205]

When it should be:

255 0
0   171
0   205
255 0
0   171
0   205
255 0
0   171
0   205
255 0
0   171
0   205
255 0
0   171
0   205
255 0
0   171
0   205
255 0
0   171
0   205
255 0
0   171
0   205
255 0
0   171
0   205
255 0
0   171
0   205

I hope that makes sense.

Steve Edwards
  • 113
  • 1
  • 5
  • 14
  • What was the output of the last thing you tried, and in what way did it not work? – kojiro Jul 23 '12 at 21:20
  • 1
    Also, this is apparently a deliberate repost of http://stackoverflow.com/questions/11605796/exporting-a-list-to-a-csv-space-separated-and-each-sublist-in-its-own-column - You should just update that question and delete this one. – kojiro Jul 23 '12 at 21:21
  • I though you said you had row and column data? The example data you posted is just a single column. Is that right? Or should it be 255,0,0\n 255,0,0\n etc. – Brendan Abel Jul 24 '12 at 02:23
  • Sorry for the confusion. I've added another column in the post for better illustration of what I'm trying to do. – Steve Edwards Jul 24 '12 at 02:57

2 Answers2

0

Your last solution should work just fine. What error did you get?

To convert to xml, you can simply create a dictionary for each "row" and use one of the solutions listed here: Serialize Python dictionary to XML.

Community
  • 1
  • 1
lbolla
  • 5,387
  • 1
  • 22
  • 35
  • I didn't get an error. The data is still not being saved the way I need it. I was wondering if I needed to create a dictionary. I may try that and see what happens – Steve Edwards Jul 24 '12 at 14:35
0

XML isn't really a row and column based format like csv; it's hierarchical, and probably not the best format for storing row/column data. You would need tags to specify rows and columns, and if you're importing this into another application, it would need to know what the row and column tags were to parse it correctly.

Is the second application written in python as well? If so, just use the python pickle module to serialize your 2D python list. That way your 2nd app can just read it back in as a 2D list, instead of marshaling it back and forth to csv or xml.

Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
  • I'm not sure what the other application was written in. Basically, I'm creating the values for the other windows-based program to import. My example data above only has 60 values. The "real-life" application will have much more information to manipulate (in the thousands.) – Steve Edwards Jul 24 '12 at 15:06