Beautiful Soup iterator question....

Paul McGuire ptmcg at austin.rr.com
Fri Apr 20 15:16:47 EDT 2007


On Apr 20, 2:05 pm, Steve Holden <s... at holdenweb.com> wrote:
<snip>
>
> did you try something like (untested)
>
> cell1, cell2, cell3, cell4, cell5, \
>                 cell6, cell7, cell8 = row.findAll("td")
>
> No need for the "for" if you want to handle each cell differently, you
> won;t be iterating over htem . And, as you saw, it doesn't work unless
> row.findAll(...) returns a sequence of eight-item containers.
>

One defensive approach to handle rows that might have too few or too
many elements, is to construct a larger list, and then slice the right
number of elements from it.

cell1, cell2, cell3, cell4, cell5, \
                cell6, cell7, cell8 = (row.findAll("td") + [None]*8)[:
8]

-- Paul





More information about the Python-list mailing list