简体   繁体   中英

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). When this writes to the file I get 252 columns and 200 rows. I want the 200 data in columns and the 252 data in row.

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 name cells by "A2" to mean column A (first column) and row 2. Column goes first. This is like a convention in all Excel APIs.

Try write_row() instead of write_column() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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