Excel中的Python输出

Excel中的Python输出,excel,python-3.x,xlsxwriter,Excel,Python 3.x,Xlsxwriter,尝试使用xlsxwriter在excel中获得笛卡尔积的输出,并按列排列,但没有成功 预期结果: 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

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

预期结果:

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()
如果第一个输出为(A、B、C、D、E),则输出应以以下方式显示在excel中:

第0行,第0列=A

第0行第1列=B

第0行第2列=C

第0行第3列=D

第0行第4列=E

然后以相同的方式显示行+=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()

您是否尝试查看
x
包含的内容?当我运行您的代码时,它只是一个包含
的字符串,这显然是非常错误的。您是否缺少
for
语句中的
?list=[(p,q,r,s,t)对于list1中的p对于list2中的q对于list3中的r对于list4中的s对于list5中的t]看起来您没有增加
for
循环中的
col
值。结果,他们(苹果除外)都在写同一个专栏。如果您在
for
循环中定义了列,并且在所有写入之间有一个col+=1,则会将它们放在不同的列中。您也可以硬编码列号。尝试更改后,现在显示TypeError:“module”对象不可调用。E1102:xlsxwriter.工作簿不可调用。我遗漏了什么吗?尝试运行它,但它显示E1102:xlsxwriter.工作簿不可调用。查看文档,它应该是大写W的工作簿。试试我的编辑器,这太棒了。非常感谢。:)嘿@Dan你能看看这个吗: