Python 输出 Excel

Python output in Excel

尝试使用 xlsxwriter 在 excel 中获取笛卡尔积的输出并按列排列它们但没有成功。

预期结果:

如果第一个输出是 (A,B,C,D,E),则输出应按以下方式在 excel 中显示:

第 0 行,第 0 列 = A

第 0 行,第 1 列 = B

第 0 行,第 2 列 = C

第 0 行,第 3 列 = D

第 0 行,第 4 栏 = E

然后row+=1和下一组结果以同样的方式显示。

代码:

list1 = ['A', 'B', 'C', 'D', 'E', 'F']
list2 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I']
list3 = ['A', 'B', 'C', 'D', 'E']
list4 = ['A', 'B', 'C', 'D']
list5 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I','J','K','L','M']

list = [(p,q,r,s,t) for p in list1 for q in list2 for r in list3 for s in 
        list4 for t in list5]

x = print(list)

import xlsxwriter
workbook = xlsxwriter.workbook('Stat.xlsx')
worksheet = workbook.add_worksheet()

row = 0
col = 0

for Apple, Boy, Cat, Dog, Eagle in (x):
  worksheet.write (row, col, Apple)
  worksheet.write(row, col + 1, Boy)
  worksheet.write(row, col + 1, Cat)
  worksheet.write(row, col + 1, Dog)
  worksheet.write(row, col + 1, Eagle)
  row += 1
workbook.close()

这是你想要的吗?

list1 = ['A', 'B', 'C', 'D', 'E', 'F']
list2 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I']
list3 = ['A', 'B', 'C', 'D', 'E']
list4 = ['A', 'B', 'C', 'D']
list5 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I','J','K','L','M']

# You probably could use product from itertools for this: https://docs.python.org/2/library/itertools.html#itertools.product
list_combined = [(p,q,r,s,t) for p in list1 
                             for q in list2 
                             for r in list3 
                             for s in list4 
                             for t in list5]

import xlsxwriter
workbook = xlsxwriter.Workbook('Stat.xlsx')
worksheet = workbook.add_worksheet()

for row, group in enumerate(list_combined):
    for col in range(5):
        worksheet.write (row, col, group[col])
workbook.close()