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)
2
  • +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. Mar 28, 2011 at 11:56
  • 3
    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".
    – Katriel
    Mar 28, 2011 at 12:05
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)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.