如何在Python中导出二维列表以实现卓越效果? [英] How do I export a two dimensional list in Python to excel?

查看:59
本文介绍了如何在Python中导出二维列表以实现卓越效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的列表:

I have a list that looks like this:

[[[u'example', u'example2'], [u'example', u'example2'], [u'example', u'example2'], [u'example', u'example2'], [u'example', u'example2']], [[5.926582278481011, 10.012500000000001, 7.133823529411763, 8.257352941176471, 7.4767647058823545]]]

我想通过以下方式将此列表保存到Excel文件中:

I want to save this list to an Excel file in the following way:

第1列:[示例,示例,...,示例]

Column 1: [example, example, ..., example]

第2列:[example2,example2,...,example2]

Column 2: [example2, example2, ..., example2]

第3列:[5.926582278481011,10.012500000000001,...,7.4767647058823545]

Column 3: [5.926582278481011, 10.012500000000001, ..., 7.4767647058823545]

推荐答案

  1. 我通过添加空格和换行符来修改数据布局,以使内容更加清晰,尽管结果在内存中与输入数据完全相同.我将其分配给一个变量以使用它.
  2. 您的数据不是二维的:它具有三个维度.我们可以将尺寸表称为表格,行和列.您指定的结果是一张五行三列的纸.第二张工作表中的数据(数字)需要放入第一张工作表的列中.第一个for循环就是这样做的.然后,为可变工作表分配数据中两张工作表中第一张工作表中的值.
  3. 您在Python中构建的工作表仅包含数据,因此您不需要Excel程序包即可创建XLS文件.如果只有数据,创建CSV文件会更容易.
  4. 运行程序.
  5. 要在Excel中打开结果文件,请双击此代码创建的export.csv文件.Excel 2013(可能是更高版本)在文件"菜单上没有导入,因此请单独打开文件,选择它,然后复制并粘贴到工作表中.

  1. I revised your data layout for clarity by adding spaces and newlines, though the result, in memory, is exactly the same as your input data. I assigned it to a variable to work with it.
  2. Your data is not two dimensional: it has three dimensions. We can call the dimensions sheet, row, and column. Your specified result is one sheet of five rows and three columns. The data from the second sheet (the numbers) needs to be put into columns of the first sheet. The first for loop does that. Then, the variable sheet is assigned the values from the first of the two sheets in your data.
  3. The sheet you are building in Python has only data, so you don't need the Excel package to create an XLS file. It's easier to create a CSV file if there is only data.
  4. Run the program.
  5. To open the resulting file in Excel, double-click on the export.csv file created by this code. Excel 2013 (and presumably later) have no import on the File menu, so open the file separately, select it, copy, and paste into the sheet where you want it to go.

import csv

yourdata = [    
        [   
            [u'example', u'example2'], 
            [u'example', u'example2'], 
            [u'example', u'example2'], 
            [u'example', u'example2'], 
            [u'example', u'example2']
        ], 
        [
            [   5.926582278481011, 
                10.012500000000001, 
                7.133823529411763, 
                8.257352941176471, 
                7.4767647058823545
            ]
        ]
]

for i in range(len(yourdata[0])):
    yourdata[0][i].append(yourdata[1][0][i])

sheet0 = yourdata[0]

newFile = open('export.csv','w',newline='')
newWriter = csv.writer(newFile, dialect='excel')
for i in range(len(sheet0)):
    newWriter.writerow(sheet0[i])
newFile.close()

这篇关于如何在Python中导出二维列表以实现卓越效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆