[TriPython] mutliple iterations in python

Beth Singley beth.singley at gmail.com
Thu Aug 4 12:03:13 EDT 2016


Thanks all!

After further investigation (and reassurance I'm not crazy), I found the
problem, which was actually a break statement in the second loop that was
firing on the first row and preventing the second loop from progressing. I
swapped for a 'continue' instead of break. Should have the issue fixed now.

Thanks again.

Beth Singley

beth.singley at gmail.com

On Thu, Aug 4, 2016 at 11:36 AM, Brian Gerard <bgerard at gmail.com> wrote:

> It sounds to me like get_sheet() returns a generator, which as you have
> seen,
> may only be looped through once.  A la...
>
> |>>> a = (i for i in range(4))
> |>>> a
> <generator object <genexpr> at 0x7f91511a8cf0>
> |>>> for x in a:
> ...  print(x)
> ...
> 0
> 1
> 2
> 3
> |>>> for x in a:
> ...  print(x)
> ...
> |>>>
>
> That's more memory efficient than loading everything into a list, but you
> can
> only go through once, and it's not subscriptable...
>
> |>>> a[3]
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: 'generator' object is not subscriptable
>
>
> You can either re-create the generator (basically resetting it), or use a
> list
> as has been suggested...
>
> |>>> a = list(i for i in range(4))
> |>>> a
> [0, 1, 2, 3]
> |>>> for x in a:
> ...  print(x)
> ...
> 0
> 1
> 2
> 3
> |>>> for x in a:
> ...  print(x)
> ...
> 0
> 1
> 2
> 3
> |>>>
>
> HTH-
> Brian
>
>
> On 08/04/2016 11:10 AM, Beth Singley wrote:
> >    Hello group,
> >    Here's a python question I'm hoping someone can explain. (I'm headed
> to
> >    stackoverflow next)
> >    I'm writing a couple of scripts to automate some common tasks our
> users
> >    are doing in Smartsheets (an online Excel 'equivalent'). I often need
> to
> >    iterate through the rows on the sheet, which are housed as a list of
> rows
> >    on the sheet object.
> >    However, when I want to run through the same sheet a second time in
> the
> >    same script, I find that the second loop won't work unless I create a
> >    brand new variable to hold a new instance of the same sheet. (Code
> snippet
> >    below). It's as if the pointer is stuck at the bottom row after the
> first
> >    loop. My solution seems awkward to me, adding an extra call and
> slowing
> >    down the whole script. Am I missing something? How do I start over at
> the
> >    top of the sheet's rows?
> >    Code snippet -
> >    sheet1= smartsheet.Sheets.get_sheet(sid)
> >    for row in sheet1.rows:
> >        # collect information from each row for some other purpose
> >    #transform information collected...
> >    for row in sheet1.rows:
> >        print(row.cells[0].value) <-- this will not print anything out
> unless
> >    I create a whole new sheet object
> >        #do something else to the rows based on information gathered and
> >    transformed between loops.
> >    Any guidance appreciated!
> >    All the best,
> >    Beth
> >    [1]beth.singley at gmail.com
> >
> > References
> >
> >    Visible links
> >    1. mailto:beth.singley at gmail.com
> >
> >
> >
> > _______________________________________________
> > TriZPUG mailing list
> > TriZPUG at python.org
> > https://mail.python.org/mailman/listinfo/trizpug
> > http://tripython.org is the Triangle Python Users Group
> >
>
> _______________________________________________
> TriZPUG mailing list
> TriZPUG at python.org
> https://mail.python.org/mailman/listinfo/trizpug
> http://tripython.org is the Triangle Python Users Group
>
-------------- next part --------------
   Thanks all!
   After further investigation (and reassurance I'm not crazy), I found the
   problem, which was actually a break statement in the second loop that was
   firing on the first row and preventing the second loop from progressing. I
   swapped for a 'continue' instead of break. Should have the issue fixed
   now. 
   Thanks again. 
   Beth Singley
   [1]beth.singley at gmail.com
   On Thu, Aug 4, 2016 at 11:36 AM, Brian Gerard <[2]bgerard at gmail.com>
   wrote:

     It sounds to me like get_sheet() returns a generator, which as you have
     seen,
     may only be looped through once.  A la...

     |>>> a = (i for i in range(4))
     |>>> a
     <generator object <genexpr> at 0x7f91511a8cf0>
     |>>> for x in a:
     ...  print(x)
     ...
     0
     1
     2
     3
     |>>> for x in a:
     ...  print(x)
     ...
     |>>>

     That's more memory efficient than loading everything into a list, but
     you can
     only go through once, and it's not subscriptable...

     |>>> a[3]
     Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
     TypeError: 'generator' object is not subscriptable

     You can either re-create the generator (basically resetting it), or use
     a list
     as has been suggested...

     |>>> a = list(i for i in range(4))
     |>>> a
     [0, 1, 2, 3]
     |>>> for x in a:
     ...  print(x)
     ...
     0
     1
     2
     3
     |>>> for x in a:
     ...  print(x)
     ...
     0
     1
     2
     3
     |>>>

     HTH-
     Brian

     On 08/04/2016 11:10 AM, Beth Singley wrote:
     >    Hello group,
     >    Here's a python question I'm hoping someone can explain. (I'm
     headed to
     >    stackoverflow next)
     >    I'm writing a couple of scripts to automate some common tasks our
     users
     >    are doing in Smartsheets (an online Excel 'equivalent'). I often
     need to
     >    iterate through the rows on the sheet, which are housed as a list
     of rows
     >    on the sheet object.
     >    However, when I want to run through the same sheet a second time in
     the
     >    same script, I find that the second loop won't work unless I create
     a
     >    brand new variable to hold a new instance of the same sheet. (Code
     snippet
     >    below). It's as if the pointer is stuck at the bottom row after the
     first
     >    loop. My solution seems awkward to me, adding an extra call and
     slowing
     >    down the whole script. Am I missing something? How do I start over
     at the
     >    top of the sheet's rows?
     >    Code snippet -
     >    sheet1= smartsheet.Sheets.get_sheet(sid)
     >    for row in sheet1.rows:
     >        # collect information from each row for some other purpose
     >    #transform information collected...
     >    for row in sheet1.rows:
     >        print(row.cells[0].value) <-- this will not print anything out
     unless
     >    I create a whole new sheet object
     >        #do something else to the rows based on information gathered
     and
     >    transformed between loops.
     >    Any guidance appreciated!
     >    All the best,
     >    Beth
     >    [1][3]beth.singley at gmail.com
     >
     > References
     >
     >    Visible links
     >    1. mailto:[4]beth.singley at gmail.com
     >
     >
     >
     > _______________________________________________
     > TriZPUG mailing list
     > [5]TriZPUG at python.org
     > [6]https://mail.python.org/mailman/listinfo/trizpug
     > [7]http://tripython.org is the Triangle Python Users Group
     >

     _______________________________________________
     TriZPUG mailing list
     [8]TriZPUG at python.org
     [9]https://mail.python.org/mailman/listinfo/trizpug
     [10]http://tripython.org is the Triangle Python Users Group

References

   Visible links
   1. mailto:beth.singley at gmail.com
   2. mailto:bgerard at gmail.com
   3. mailto:beth.singley at gmail.com
   4. mailto:beth.singley at gmail.com
   5. mailto:TriZPUG at python.org
   6. https://mail.python.org/mailman/listinfo/trizpug
   7. http://tripython.org/
   8. mailto:TriZPUG at python.org
   9. https://mail.python.org/mailman/listinfo/trizpug
  10. http://tripython.org/


More information about the TriZPUG mailing list