getlist question
MRAB
python at mrabarnett.plus.com
Thu Dec 24 18:32:10 EST 2009
Victor Subervi wrote:
> On Thu, Dec 24, 2009 at 3:28 PM, MRAB <python at mrabarnett.plus.com
> <mailto:python at mrabarnett.plus.com>> wrote:
>
> Victor Subervi wrote:
>
> Hi;
> I have the following code:
>
> try:
> trueVal = form.getlist(storeColNames[i])
> colNames.append(storeColNames[i])
> if len(trueVal) > 1:
> trueVal = string.join(trueVal, ',')
>
>
> Unless you're using a very old version of Python, you should be using
> the string method:
>
> trueVal = ','.join(trueVal)
>
>
> values.append(trueVal)
> elif len(trueVal) == 1:
> print storeColNames[i], trueVal, '<br />'
> trueVal = '%s' % trueVal[0]
> values.append(trueVal)
> if len(trueVal) > 0:
> sql = '%s="%s"' % (storeColNames[i], trueVal)
> sqlUpdate.append(sql)
> except:
> raise
>
> This works fine except when storeColNames[i] returns no data.
> Now, if I were dealing with getfirst instead of getlist, I could
> easily put in a nonsense default data value such as '%$#' and
> check for that. But how can I do that or something similar (and
> preferably more elegant) with getlist, which takes only the one
> name parameter?
>
> You just need to check whether len(trueVal) == 0. Simple.
>
>
> The problem is that it doesn't see the value at all
>
> trueVal = form.getlist(storeColNames[i])
> test = ','.join(trueVal)
> if len(test) == 0:
> trueVal == ''
>
> It simply doesn't register storeColNames[i] if there is nothing provided
> from the referring page. Here's a test printout:
>
[snip]
> You can see from the above part with breaks that "Availability" isn't
> logged. But it is in the insert statement...without a value, which
> throws an error. The complete code follows:
>
[snip]
Try working through the code by hand for that value.
More information about the Python-list
mailing list