Python 到 Excel,具有多个元素

问题描述 投票:0回答:1

编码新手。 我有一个关于 Python 中的列表并将所述列表放入 Excel 文件的问题。我的列表类似于以下。

List1 =
[[[a,1], [b,2], [c,3], [d,4], [e,5]],
[[aa,11], [bb,22], [cc,33], [dd,44],[ee,55]],
[[aaa,111], [bbb,222], [ccc,333], [ddd,444],[eee,555]],
[[aaaa,1111], [bbbb,2222], [cccc,3333], [dddd,4444],[eeee,5555]],

假设我的列表比这个大得多,但每个列表中的元素数量与上面所示相同。现在我的目标是让它保存并超越这样的东西:(https://i.sstatic.net/MBnNhHHp.png)

对加载向下或向侧面加载的方向不太感兴趣,我想看看 Excel 中的列表就是全部。谢谢您的帮助

我什至不知道从哪里开始

python python-3.x excel list xlsx
1个回答
0
投票

csv 模块提供了一种简单的方法来创建包含 Excel 将加载的逗号分隔列值行的文本文件:

import csv

list1 = [[['a',1], ['b',2], ['c',3], ['d',4], ['e',5]],
         [['aa',11], ['bb',22], ['cc',33], ['dd',44],['ee',55]],
         [['aaa',111], ['bbb',222], ['ccc',333], ['ddd',444],['eee',555]],
         [['aaaa',1111], ['bbbb',2222], ['cccc',3333], ['dddd',4444],['eeee',5555]]]

# open with newline='' (csv.writer requirement)
with open('output.csv', 'w', newline='') as fout:
    # wrap open file in a CSV writer.
    writer = csv.writer(fout)
    # Add headers.  Each row is a list of column values
    writer.writerow(['column1', 'column2'])
    # Iterate the list of list of lists:
    for lis in list1:
        # writerows takes a list of lists, and treated as rows of columns
        writer.writerows(lis)
        # write a blank row between lists
        writer.writerow([])

输出(文本):

column1,column2
a,1
b,2
c,3
d,4
e,5

aa,11
bb,22
cc,33
dd,44
ee,55

aaa,111
bbb,222
ccc,333
ddd,444
eee,555

aaaa,1111
bbbb,2222
cccc,3333
dddd,4444
eeee,5555

输出(Excel):

Excel image of loaded CSV

© www.soinside.com 2019 - 2024. All rights reserved.