xlwt横向插入表格数据

有数据data:
[(1,),(2,),(3,)],[('a',),('b',),('c',)],[('e',),('f',),('g',)]分别代表三种不同的列的值。
需求是:横向写入表格记录:
横向插入

我的程序如下,只能实现数列插入,不知道更改如何修改插入的顺序,求指导。数列插入

import xlwt

wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet('Sheet1', cell_overwrite_ok=True)

data = (
[(1,),(2,),(3,)],
[('a',),('b',),('c',)],
[('e',),('f',),('g',)],
)
for index, value in enumerate(data):
    for r_num, r_value in enumerate(value):
        ws.write(r_num, index,r_value[0])

wb.save('test.xls')
阅读 3.6k
2 个回答

ws.write(index, r_num,r_value)

import xlwt

wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet('Sheet1', cell_overwrite_ok=True)

data_old = (
[(1,),(2,),(3,)],
[('a',),('b',),('c',)],
[('e',),('f',),('g',)],
)

data = [i for i in map(list,zip(*data_old))]

col = len(data)
row = len(data[0])

for r_row in range(row):
    for r_col in range(col):
        ws.write(r_row,r_col,data[r_row][r_col][0])

wb.save('test.xls')
推荐问题
宣传栏