0

Is it possible to do something like this?

for i in range(1,8):
    worksheet+i = gSheet.wks.get_worksheet(i)
    presentations = getActivity(query, getDate(i))
    exportToSheet(query, worksheet+i, presentations)

I need the counter to be concatenated with the work worksheet to form a new variable name every time it loops.

Currently giving me this error:

  File "dagklapper.py", line 99
  worksheet+i = gSheet.wks.get_worksheet(i)
  SyntaxError: can't assign to operator
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Martijn Kerckhaert
  • 494
  • 1
  • 6
  • 21

2 Answers2

4

The code you have shown doesn't seem to need variables worksheet1 to worksheet8 at all, you could just reuse worksheet.

for i in range(1, 8):
    worksheet = gSheet.wks.get_worksheet(i)
    presentations = getActivity(query, getDate(i))
    exportToSheet(query, worksheet, presentations)

If you need to access the worksheets later in your code, you could create a dictionary worksheets. Then you could access the worksheets with worksheets[1], worksheets[2] and so on.

worksheets = {}
for i in range(1, 8):
    worksheets[i] = gSheet.wks.get_worksheet(i)
    presentations = getActivity(query, getDate(i))
    exportToSheet(query, worksheets[i], presentations)
Alasdair
  • 298,606
  • 55
  • 578
  • 516
1

at this:- worksheet+i = gSheet.wks.get_worksheet(i)

When you use an assignment operator, you assign the value of what is on the right to the variable or element on the left. In your case, there is no variable or element on the left, but instead an interpreted value: you are trying to assign a value to something that isn't a "container".

from here syntax-error: can't assign to operator

Community
  • 1
  • 1
Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24