如何将python数组写入单个excel电子表格单元格

4ioopgfo  于 2021-09-08  发布在  Java
关注(0)|答案(2)|浏览(454)

我尝试使用python将以下数组写入excel电子表格单元格:

array = [ [a1,a2,a3], [a4,a5,a6], [a7,a8,a9], [a10, a11, a12, a13, a14]]

这是我现在正在使用的代码:

import xlsxwriter

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

array = [['a1', 'a2', 'a3'],
         ['a4', 'a5', 'a6'],
         ['a7', 'a8', 'a9'],
         ['a10', 'a11', 'a12', 'a13', 'a14']]

row = 0

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

workbook.close()

这是我得到的输出:

我希望它像这样打印:

ocebsuys

ocebsuys1#

使用 join()set_text_wrap() https://xlsxwriter.readthedocs.io/workbook.html#workbook-添加formathttps://xlsxwriter.readthedocs.io/format.html#set_text_wrap

import xlsxwriter

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

cell_format = workbook.add_format()
cell_format.set_text_wrap()
cell_format.set_align('vcenter')

array = [['a1', 'a2', 'a3'],
         ['a4', 'a5', 'a6'],
         ['a7', 'a8', 'a9'],
         ['a10', 'a11', 'a12', 'a13', 'a14']]

row = 0

for col, data in enumerate(array):
    worksheet.write(row, col, '\n'.join(data), cell_format)

workbook.close()
0s7z1bwu

0s7z1bwu2#

我认为你应该使用 "\n" 将它们全部存储在一个单元格中。
试着这样说:

array = ['\n'.join(i) for i in array]

相关问题