I have a need of unpacking two-dimensional list (list of lists) with a dynamic length. The whole task is to form a full-featured HTML-table from my list with the help of lxml framework.
Having been used this excellent answer as a backbone I came to the following code for my task:
page = (
E.html(
E.body(
E.table(
E.tr(
E.th(E.div("header1")),
...
E.th(E.div("header40")),
),
*[E.tr(
*[
E.td(str(col)) for col in p_list[1] <<- how to put N here???
]
) for row in range(len(p_list))]
, border="2"
)
)
)
)
The first E.tr
is a header for my table, the second one *[E.tr
is unpacked from the list p_list
. The list has N elements (aka rows) each of which is a list itself consisting of around 50 elements (aka columns of N row).
The p_list
is declared and filled like this:
p_list = list()
rows = table.iter('div')
p_list.append([c.text for c in rows])
rows = table.xpath("body/table")[0].findall("tr")
for row in rows[2:]:
p_list.append([c.text for c in row.getchildren()])
Now this code can only output the same row of p_list
N times and works only with a hard-coded row number, but what if I want to specify it dynamically to output the whole table?
Cannot figure out how to do this.
Replace p_list[1]
with p_list[row]
. Even better, don't use range
:
*[E.tr(
*[ E.td(str(col)) for col in row ]
) for row in p_list ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.