简体   繁体   English

如何将 Python 数组写入单个 Excel 电子表格单元格

[英]How to Write Python array into a Single Excel Spread Sheet Cell

I am trying to write following array into an Excel spreadsheet cell using Python:我正在尝试使用 Python 将以下数组写入 Excel 电子表格单元格中:

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

this is the code I am using right now:这是我现在使用的代码:

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() 

This is the Output I am getting:这是我得到的输出:

这是我得到的输出

I want it to print it like this:我希望它像这样打印:

在此处输入图片说明

Using join() and set_text_wrap()使用join()set_text_wrap()

https://xlsxwriter.readthedocs.io/workbook.html#workbook-add-format https://xlsxwriter.readthedocs.io/format.html#set_text_wrap https://xlsxwriter.readthedocs.io/workbook.html#workbook-add-format https://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() 

I think you should use a "\\n" to store them all in a single cell.我认为您应该使用"\\n"将它们全部存储在一个单元格中。

Try to put something like this:试着把这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM