Parsing a list of items - how-to?

25 views
Skip to first unread message

Larry Wapnitsky

unread,
Feb 22, 2012, 3:24:07 PM2/22/12
to web...@googlegroups.com
Disclaimer:  I'm new to Web2py (2 days and loving it) and re-learning HTML after many years away from it.

I have a large text field on a form whose input will be a list of items entered by the user, separated by some sort of delimiter (most likely a newline).  Each valid line item will be entered into the database as its own entity.

How do I go about parsing this text box?  Everything I've seen so far talks about fields that come from/go to a database, but  this one doesn't do so directly.

Let me know if I need to clarify.

Thanks,
Larry

Alan Etkin

unread,
Feb 22, 2012, 5:01:48 PM2/22/12
to web2py-users
I think that defining a Field in the model as "list: string" give you
that functionality without further configuration (just creating a
common web2py form intance). For using new lines you should use the
TEXTAREA tag (used for "text" type fields), and split the input string
with the Python built-in string manipulation functions at the
controller.

Anthony

unread,
Feb 22, 2012, 5:07:16 PM2/22/12
to web...@googlegroups.com
Maybe something like this:

mylist = request.vars.mytextarea.splitlines()

That assumes a newline is the delimiter. If you use a different delimiter, then use .split(delimiter) instead.

Anthony

Larry Wapnitsky

unread,
Feb 23, 2012, 5:39:40 AM2/23/12
to web...@googlegroups.com
Thanks to both of you.  I'll try these when I get to my office this morning.  Assume I'll be back with either good results or more questions :)

Reply all
Reply to author
Forward
0 new messages