我是Python的新手,我尝试使用此简单代码,一切正常,但顺序不会重复。请帮我。

this is the excel output

import xlsxwriter

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

expenses = (
    ['Rent', 1000],
    ['Gas',   100],
    ['Food',  300],
    ['Gym',    50],
)

row = 0
col = 0

for item, cost in (expenses):
    worksheet.write(row, col,     item)
    worksheet.write(row, col + 1, cost)
    row += 1


    workbook.close()

最佳答案

您可能在第一次迭代后关闭了工作簿。

取消缩进最后一行:

import xlsxwriter

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

expenses = (
    ['Rent', 1000],
    ['Gas',   100],
    ['Food',  300],
    ['Gym',    50],
)

row = 0
col = 0

for item, cost in (expenses):
    worksheet.write(row, col,     item)
    worksheet.write(row, col + 1, cost)
    row += 1


workbook.close()  # mistake here

关于python - Python Xlsxwriter无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50447854/

10-12 18:15