简体   繁体   English

将列表导出为CSV或XML,并将每个子列表导出到自己的列中-重新发布

[英]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. 我正在尝试将列表导出到CSV列中。 (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. (基本上,这是另一个程序将能够使用我生成的数据的方式。)我有一个名为[frames]的组,其中包含[frame001],[frame002],[frame003]等。我想要CSV文件生成的结果将在第一列中包含[frame001]的所有值,在第二列中具有[frame002]的所有值,依此类推。 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. 我以为如果可以将文件另存为CSV,就可以在Excel中进行操作,但是,我认为有一种解决方案,我可以编程以跳过该步骤。

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. 经过更多研究之后,我实际上需要构建一个XML文件,但是我仍然需要数据顺序正确。 That is, the contents of [frame001] in a column, [frame002] in the next column, etc. All frames contain the same amount of information. 也就是说,一列中[frame001]的内容,下一列中[frame002]的内容,等等。所有帧都包含相同数量的信息。

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] 这是前两列的数据示例:[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,您只需为每个“行”创建一个字典,然后使用此处列出的解决方案之一:将Python字典序列化为XML

XML isn't really a row and column based format like csv; XML实际上不是像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? 第二个应用程序也是用python编写的吗? If so, just use the python pickle module to serialize your 2D python list. 如果是这样,只需使用python pickle模块来序列化2D python列表。 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. 这样,您的第二个应用程序就可以将其作为2D列表读回,而无需将其前后组合到csv或xml中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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