How to iterate through column items and display its rows data

58 views
Skip to first unread message

Maurice Waka

unread,
Apr 30, 2018, 9:35:25 AM4/30/18
to web2py-users
If I have a column with data like db.persons with data like
Carl
Junior
Maggie
Tom
Derrick

And each column name has other fields with data such as:
persons.name persons.age persons.location persons.occupation persons.interests
Carl 23 London Neuroscientist brainy stuff
Junior 25 Tokyo doctor medical research
Maggie 33 Nairobi farmer GMO research
Tom 25 Sydney teacher educational ideas
Derrick 21 Chicago surgeon Cutting through
 


I want to iterate through the column  persons.name and randomly pick a name and using the rows object, display all data about the person.

If I do 
rows_name = db().select(db.persons.name).column()

rows
= db(db.persons.ALL).select()
for row in rows:
   
if rows_name[0] == 'Maggie':
       
for row in rows:
               
return [row.age+' '+row.location+' '+row.occupation+' '+row.interests]

The problem is that if I query 'Carl' which is the first item, I get the response but I cant query any other name .
Regards
        
    

pbreit

unread,
Apr 30, 2018, 3:10:46 PM4/30/18
to web...@googlegroups.com
I would think something like:

rows = db(db.person.id>0).select()
random_number
= random.randrange(0, rows.count())
row
= rows[random_number]
return row

If you wanted to return it as a formatted string, maybe:

return '%s, %s, %s, %s' % (row.name, row.age, row.location, row.occupation)

Maurice Waka

unread,
Apr 30, 2018, 11:03:45 PM4/30/18
to web...@googlegroups.com
I get this error : AttributeError: 'Rows' object has no attribute 'count' 

On Mon, 30 Apr 2018, 22:10 pbreit <pbreit...@gmail.com> wrote:
I would think something like:

rows = db(db.person.id>0).select()
random_number
= random.randrange(0, rows.count())
row
= rows[random_number]
return row

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/ynBOuIdufso/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sandeep Patel

unread,
Apr 30, 2018, 11:41:57 PM4/30/18
to web...@googlegroups.com
 Try this
rows = db(db.person.id>0).count()


To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.

pbreit

unread,
Apr 30, 2018, 11:55:06 PM4/30/18
to web2py-users
Very sorry. Try:

random_number = random.randrange(0, len(rows))

Maurice Waka

unread,
May 1, 2018, 12:03:17 AM5/1/18
to web...@googlegroups.com
Thanks. I got the count. 

On Tue, 1 May 2018, 06:41 Sandeep Patel <san...@robodia.com> wrote:
 Try this
rows = db(db.person.id>0).count()

On Tue, May 1, 2018 at 8:33 AM, Maurice Waka <mauri...@gmail.com> wrote:
I get this error : AttributeError: 'Rows' object has no attribute 'count' 

On Mon, 30 Apr 2018, 22:10 pbreit <pbreit...@gmail.com> wrote:
I would think something like:

rows = db(db.person.id>0).select()
random_number
= random.randrange(0, rows.count())
row
= rows[random_number]
return row

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/ynBOuIdufso/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/ynBOuIdufso/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Maurice Waka

unread,
May 1, 2018, 12:04:57 AM5/1/18
to web...@googlegroups.com
Now another error :invalid syntax: random _number = random.randrange(0,rows.count())

Dave S

unread,
May 1, 2018, 4:01:05 PM5/1/18
to web2py-users


On Monday, April 30, 2018 at 9:04:57 PM UTC-7, Maurice Waka wrote:
Now another error :invalid syntax: random _number = random.randrange(0,rows.count())


Is the typo just in your post, or in the code you are trying?  (Remove space between "random" and "_number").

/dps

pbreit

unread,
May 1, 2018, 6:06:26 PM5/1/18
to web2py-users
I am very sorry, I need to be more careful with my replies.

I wasn't sure if random needed to be imported but it looks like it does. So:

import random

rows = db(db.person.id>0).select()
random_number 
= random.randrange(0, len(rows))

Massimo Di Pierro

unread,
May 5, 2018, 11:03:00 PM5/5/18
to web2py-users
you know you can do

row = db(db.person.id>0).select(limitby=(0,1),orderby='<random>').first()

Maurice Waka

unread,
May 6, 2018, 12:41:14 AM5/6/18
to web...@googlegroups.com
Thanks. It worked 

Reply all
Reply to author
Forward
0 new messages