简体   繁体   中英

How to export data to xlsx using xlsxwriter for python

I am having trouble writing data to excel files. Running this code does not write the file to the expected folder.

import xlsxwriter

workbook = xlsxwriter.Workbook('C:\\Users\\hburk\\Desktop\\Python\\sqloutput.xlsx')
east = workbook.add_worksheet()



eastdata = (
        ['apples', 4.0],
        ['bananas', 5.0],
        )

    row = 1
    col = 0
    for pointname, price in eastdata:
        east.write_string(row, col, pointname)
        east.write(row, col+1, price)
        row += 1

workbook.close()

Edits: I changed the silly errors: eastres and east.close() - I tried to simplify this from the original code and mucked some things up. The code still doesn't work as written here.

只是不要忘记关闭Workbook

workbook.close()

The code still doesn't work as written here.

If you fix the indentation it should work. I ran this:

import xlsxwriter

workbook = xlsxwriter.Workbook('sqloutput.xlsx')
east = workbook.add_worksheet()


eastdata = (
    ['apples', 4.0],
    ['bananas', 5.0],
)

row = 1
col = 0
for pointname, price in eastdata:
    east.write_string(row, col, pointname)
    east.write(row, col+1, price)
    row += 1

workbook.close()

And got this:

在此处输入图片说明

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