导出到Excel时数据显示错误

所以我有这段代码:

import itertools
import xlsxwriter
from pprint import pprint

inputdata = [
    ['900x200', '200x500', '232x232'],
    ['242423', '232d2s', '2sdad'],
    ['AA', 'BB'],
]
result = list(itertools.product(*inputdata))
pprint(result)

workbook = xlsxwriter.Workbook('Documents/files/arrays.xlsx')
worksheet = workbook.add_worksheet()

row = 0

for col, data in enumerate(result):
    worksheet.write_column(row, col, data)

workbook.close()

第一部分生成组合,然后将其保存到Excel文件中。

我的问题是项目显示如下: = = = = = = = = = = = = = = = = = = = = = = = =

我希望他们像: = = = = = = = = = = = = = = = = = = = = =

基本上只是“旋转”,因为当前数据没有多大意义。

评论
  • 浅浅小鸣
    浅浅小鸣 回复

    You're just not using the write function for the output you want. Change worksheet.write_column(row, col, data) to worksheet.write_row(row, col, data)

    基本上,您的新脚本应为:

    import itertools
    import xlsxwriter
    from pprint import pprint
    
    inputdata = [
        ['900x200', '200x500', '232x232'],
        ['242423', '232d2s', '2sdad'],
        ['AA', 'BB'],
    ]
    result = list(itertools.product(*inputdata))
    pprint(result)
    
    workbook = xlsxwriter.Workbook('arrays.xlsx')
    worksheet = workbook.add_worksheet()
    
    col = 0
    
    for row, data in enumerate(result):
        worksheet.write_row(row, col, data)
    
    workbook.close()
    

    这将产生您想要的输出