[portland] Please explain this error

jason kirtland jek at discorporate.us
Wed Jan 16 20:50:59 CET 2008

Rich Shepard wrote:
>    I need to identify every item belonging to another variable. Each item has
> a sequential number which is extracted from a database table and stored in
> the tuple called 'row'. Using this code fragment:
>       for i in range(row[16]):
>          idx = i+1
>          print row[1], '\t', idx
> the results include:
>  	Sedimentation   1
>  	Sedimentation   2
>  	Sedimentation   3
>  	InfiltrationRate        1
>  	InfiltrationRate        2
>  	InfiltrationRate        3
> Traceback (most recent call last):
>    File "eikos.py", line 145, in OnProjParms
>      projectReports().inputVals()
>    File "/data1/eikos/reports.py", line 394, in inputVals
>      for i in range(row[16]):
> TypeError: range() integer end argument expected, got unicode.
>    Since the code _seems_ to work properly I don't understand why it doesn't
> end cleanly. I understand the words in the error message but not what I need
> to do to fix it.

looks like the value of row[16] is a unicode string, not a number.  you
could check it via

   assert isinstance(row[16], int)
   for i in range(row[16]):

and/or workaround it by converting to an integer:

    for i in range(int(row[16])):

More information about the Portland mailing list