1
for i in range (0,len(list4)):
  ws.write(1, 0, datetime.now(), style1)
  ws.write(2, 1, 1)
  ws.write(2, 2, xlwt.Formula("A3+B3"))
  wb.save('example.xls')

how do i generalize (A3+B3) ?

'"A" + i' + '"B" + i'  

Is this correct???

2 Answers 2

7
'A%(row)d+B%(row)d' % {'row': 3}

or

'A{0}+B{0}'.format(3)
Sign up to request clarification or add additional context in comments.

2 Comments

+1 I'm upvoting because I think the versions of the string formatting routines that you use are cleaner than my very old-fashioned ways. I prefer the first one where a meaningful name can be used.
Just to point out you can still use named fields in the newer PEP 3101 .format syntax: "A{row} + B{row}".format(row=3). The 0 just means "use the first value".
3

You need:

ws.write(2, 2, xlwt.Formula("A" + str(i) + "+B" + str(i)))

Or more idiomatically:

ws.write(2, 2, xlwt.Formula("A%d+B%d" % (i,i)))

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.