简体   繁体   中英

Exporting a list to a CSV or XML and each sublist in its own column - Repost

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM