1

I have a loop which retrieves two lists of values from some json. I would like to export each one of the values list (1000 results each one) with its header to Exscel. All values are available when I'm printing the results, but Excel sheet contains only header and a single row. Perhaps, I'm missing some loop to add. Any help is appreciated!

list = x.json() 
    
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()

for item in list:
    item1 = value1['valueID1']
    item2 = value2['valueID2']

    data = {"item1": [item1],"item2": [item2]}

    col_num = 0
    for key, value in data.items():
        worksheet.write(0, col_num, key)
        worksheet.write_column(1, col_num, value)
        col_num += 1
    workbook.close()

1 Answer 1

1

You need to increment the row value as well.

list = x.json() 
    
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()

for item in list:
    item1 = value1['valueID1']
    item2 = value2['valueID2']

    data = {"item1": [item1],"item2": [item2]}

    col_num = 0
    row_num = 0

    for key, value in data.items():
        worksheet.write(row_num , col_num, key)
        worksheet.write_column(row_num , col_num, value)
        col_num += 1
    row_num=row_num+1
workbook.close()
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, unfortunately, this didn't help.
@l3xand3r you do need to apply this fix. You also need to move the close() out of the loop. You are closing the file on the first iteration.
@I3xand3r i have edited the code as per jmcnamara comment.
No luck with this as well... Still get a single row result
Please post the sample input data for us to test

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.