简体   繁体   English

逐行写入excel文件,而不是逐列写入

[英]Write to excel file row by row, instead of column by column

I am sorry if the below question is very dumb. 如果以下问题很愚蠢,我感到抱歉。 I am trying to write an array to file. 我正在尝试将数组写入文件。 I tried a few combinations and somehow was able to successfully do this. 我尝试了几种组合,并以某种方式成功地做到了这一点。 I don't fully understand how the below work but it works. 我不完全了解以下内容的工作原理,但确实有效。

I have an array which has a shape (252, 200). 我有一个形状为(252,200)的数组。 When this writes to the file I get 252 columns and 200 rows. 当这写入文件时,我得到252列和200行。 I want the 200 data in columns and the 252 data in row. 我想要列中有200个数据,行中有252个数据。

import xlsxwriter as xls
workbook  = xls.Workbook('Lookback_Call.xlsx')
workbook = xls.Workbook('Lookback_Call.xlsx', {'nan_inf_to_errors': True})
worksheet  = workbook.add_worksheet()

row = 0
for col, data in enumerate(tst1):
   worksheet.write_column(row, col, data)

workbook.close()

Simply change 只需更改

worksheet.write_column(row, col, data)

into

worksheet.write_column(col, row, data)

will work. 将工作。 This is a common issue when working with Excel. 这是使用Excel时的常见问题。 Excel name cells by "A2" to mean column A (first column) and row 2. Column goes first. Excel名称单元格用“ A2”表示列A(第一列)和第2行。列在第一行。 This is like a convention in all Excel APIs. 这就像所有Excel API中的约定。

Try write_row() instead of write_column() : 尝试使用write_row()而不是write_column()

for row, data in enumerate(tst1):
    worksheet.write_column(row, 0, data)

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

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