0

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

2 Answers 2

2

Try write_row() instead of write_column():

for row, data in enumerate(tst1):
    worksheet.write_column(row, 0, data)
Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

Everything gets written in the same row now :(
because you forget to update the row in each iteration! You need two levels of for-loop to scan every element in a 2D array.
actually no I did this and it works worksheet.write_row(col, row, data) :) Don't ask me why, I have no idea but it does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.