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
3

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

In your first solution, last line, delete the +i
I like your solution, thank you. Although it gives me this now: TypeError: 'Worksheet' object does not support indexing
Can you edit your question and post the traceback, I can't tell the problem from that error message alone.
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

Comments

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.