[Tutor] String Substitution

Daniel Ehrenberg littledanehren at yahoo.com
Sun Jan 18 12:43:52 EST 2004


richard wrote:
> Greetings,
> 
> Once again I am stuk on strings. I  wish to get the
> text entered
> in a number of fields and pass it into a dict which
> I can then use
> in an a sql query to update a record (and vice
> versa). Each control
>   is number textCtrl0 to in this case 2.
> 
> However when I do the following:
> 
> values = []     # create the empty dict
> for i in range(3): # create the range
>       values.append (self.textCtrl%d.GetValue() % i)
> # append the contents 
> of each ctrl to 'values'
> 
> Instead of getting the following:
> 
> values = [textCtrl0.data, textCtrl1.data,
> textCtrl2.data]
> 
> I get this:
> 
> Traceback (most recent call last):
>    File "D:\pythonwork\tesguis\frmfmna.py", line
> 222, in saveButton
>      values.append (self.textCtrl%d.GetValue() % i)
> AttributeError: frmfmna instance has no attribute
> 'textCtrl'
> 
> Which is correct as there is  no control called
> textCtrl
> 
> Question is why does it not work?
> 
> Richard

You are trying to do a string operation on something
that isn't a string. Instead of writing:

values.append(self.textCtrl%d.GetValue() % i

You have to treat it as a string and then evaluate it.
Here's what you should have written:

values.append(eval('self.textCtrl%d.GetValue()' % i))

That's not very readable, and eval isn't very
efficient, so since it's just three items, you'd
probably be better off writing (instead of the entire
loop):

values.append(self.textCtrl1.GetValue())
values.append(self.textCtrl2.GetValue())
values.append(self.textCtrl3.GetValue())

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus



More information about the Tutor mailing list