无法将数字连接到变量

时间:2015-11-30 23:07:48

标签: python

我正在尝试将数字附加到我的变量上,但它无效。我也试过'。'运算符而不是'+'但不起作用。我甚至试过没有+或。 (工作表索引),这也不起作用。有点卡在这里 - 会很感激帮助。

以下是代码:

workbook = xlsxwriter.Workbook('JAY_test.xlsx')
worksheet0 = workbook.add_worksheet('Toys Ordered')
worksheet1 = workbook.add_worksheet('Toys Shipped')

for index in range(len(case_queries)):
    cur.execute(case_queries[index])
    rows = cur.fetchall()

    # Start from the first cell. Rows and columns are zero indexed.
    row = 0
    col = 0

    # Iterate over the data and write it out row by row.
    for return_date, return_hour, return_count in (rows):
        worksheet+index.write(row, col,     str(return_date))
        worksheet+index.write(row, col + 1, return_hour)
        workshweet+index.write(row, col + 2, return_count)
        row += 1

我收到的错误如下:

Traceback (most recent call last):
  File "test.py", line 38, in <module>
    worksheet+index.write(row, col,     str(return_date))
NameError: name 'worksheet' is not defined
Exception Exception: Exception('Exception caught in workbook destructor. Explicit close() may be required for workbook.',) in <bound method Workbook.__del__ of <xlsxwriter.workbook.Workbook object at 0x2173c10>> ignored

1 个答案:

答案 0 :(得分:2)

这种命名不起作用。您应该使用lists代替。

workbook = xlsxwriter.Workbook('JAY_test.xlsx')
worksheet = []
worksheet.append(workbook.add_worksheet('Toys Ordered')) # worksheet[0]
worksheet.append(workbook.add_worksheet('Toys Shipped')) # worksheet[1]

for index in range(len(case_queries)):
    cur.execute(case_queries[index])
    rows = cur.fetchall()

    # Start from the first cell. Rows and columns are zero indexed.
    row = 0
    col = 0

    # Iterate over the data and write it out row by row.
    for return_date, return_hour, return_count in (rows):
        worksheet[index].write(row, col,     str(return_date))
        worksheet[index].write(row, col + 1, return_hour)
        worksheet[index].write(row, col + 2, return_count)
        row += 1