getlist question
MRAB
python at mrabarnett.plus.com
Thu Dec 24 15:28:31 EST 2009
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.
More information about the Python-list
mailing list