在python中将列表转换为HTML表格的最简单方法? [英] Easiest way to turn a list into an HTML table in python?

查看:78
本文介绍了在python中将列表转换为HTML表格的最简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的列表:

['一','二','三','四','五','六','七','八','九']

并且我想尝试将这些数据转换为各种维度的 HTML 表格:

一二三四五六七八九

一四七二五八三六九

一二三四五六七八九

有没有一个库可以处理这个,而无需进行疯狂的列表拼接或嵌套 for 循环?我的谷歌搜索显示有一些 HTML 库,但我没有时间逐一查看它们是否可以很好地处理表格.有没有人必须这样做?如果是这样,你是怎么做到的?

解决方案

我会把你的问题分解成两部分:

  • 给定一个平面列表",生成一个子列表列表,其中子列表具有给定的长度,并且整个列表可以按照行主要"顺序(您的第一个和第三个示例)或列主要"顺序进行(你的第二个例子);
  • 给定一个包含字符串项的子列表列表,从中生成一个 HTML 表.

我认为这两个任务真的非常不同,将它们混合起来并没有什么好处(也没有很多损失),所以如果有任何设计良好的库做了这样的混合,我会感到惊讶.

对于第 1 点,行优先很容易:

def row_major(alist, sublen):返回 [alist[i:i+sublen] for i in range(0, len(alist), sublen)]

和列专业没那么糟糕:

def col_major(alist, sublen):numrows = (len(alist)+sublen-1)//sublen返回 [alist[i::sublen] for i in range(numrows)]

例如...:

L = ['一','二','三','四','五','六','七','八','九']对于 r in row_major(L, 3): 打印 r打印对于 col_major(L, 3) 中的 r:打印 rfor r in row_major(L, 4): 打印 r

产生您想要的三个结果(每行一个列表,还不是 HTML 形式;-).

问题的后半部分——从字符串列表中生成一个 HTML 表格:

def html_table(lol):打印 ''对于 lol 中的子列表:打印'
'打印'</td><td>'.join(sublist)打印'</td></tr>'打印</table>"

如果您想将其作为单个字符串获取而不是打印出来,请将每个print 更改为 yield 并使用 ' '.join(html_table(lol)).

现在你有两个简单、有用、可用和可重用的构建块——将它们分开会派上用场,只要你想将数据呈现为 HTML 表格以外的任何东西,以及当要呈现的列表列表时因为 HTML 表格来自任何其他构建方式.在您的应用程序代码中将它们放在一起很容易,但当然也很容易做一个简单的粘合例程",例如,假设基于 yieldhtml_table 版本> 并且需要单个字符串结果:

def list_to_html_table(alist, sublength, column_major=False):如果 column_major:大声笑 = col_major(alist, sublength)别的:大声笑 = row_major(alist, sublength)返回 '​​'.join(html_table(lol))

就大块糊状胶水而言,这种构建块方法是不是真的更好、更愉快、也更高效?

lets say I have a list like so:

['one','two','three','four','five','six','seven','eight','nine']

and I want to experiment with turning this data into a HTML table of various dimensions:

one     two    three
four    five   six
seven   eight  nine

or

one    four   seven
two    five   eight
three  six    nine

or

one    two   three  four
five   six   seven  eight
nine

Is there a library that can handle this without needing to do crazy list splicing or nested for loops? My google searches reveal that there are a few HTML libraries, but I don't have the time to go through each one to see if they can handle tables very well. Has anyone ever had to do this? If so how did you do it?

解决方案

I would decompose your problem into two parts:

  • given a "flat list", produce a list of sublists where the sublists are of a given length and the overall list may be walked into either a "row major" order (your first and third example) or "column major" (your second example);
  • given a list of sublists with string items, produce an HTML table out of it.

I think the two tasks are really very distinct and there's nothing to gain (and much to lose) in mushing them up, so I would be astonished if any well-designed library did such mushing.

For point 1, row-major is easy:

def row_major(alist, sublen):      
  return [alist[i:i+sublen] for i in range(0, len(alist), sublen)]

and column-major not that bad:

def col_major(alist, sublen):
  numrows = (len(alist)+sublen-1) // sublen 
  return [alist[i::sublen] for i in range(numrows)]

for example...:

L = ['one','two','three','four','five','six','seven','eight','nine']
for r in row_major(L, 3): print r
print
for r in col_major(L, 3): print r
for r in row_major(L, 4): print r

produces your three desired results (one list per row, not in HTML form yet;-).

The second half of the problem -- produce an HTML table from a list of lists of strings:

def html_table(lol):
  print '<table>'
  for sublist in lol:
    print '  <tr><td>'
    print '    </td><td>'.join(sublist)
    print '  </td></tr>'
  print '</table>'

If you want to get it as a single string rather than print it out, change each print into yield and use ' '.join(html_table(lol)).

Now you have two simple, useful, usable and reusable building blocks -- having them separate will come in handy whenever you want to present your data as anything BUT an HTML table, and also whenever the list-of-lists to present as an HTML table comes from any other way of building it. Putting them together is easy to do in your application code, but of course it's also easy to do a simple "glue routine", e.g., assuming the yield-based version of html_table and that a single string result is desired:

def list_to_html_table(alist, sublength, column_major=False):
  if column_major:
    lol = col_major(alist, sublength)
  else:
    lol = row_major(alist, sublength)
  return ''.join(html_table(lol))

Isn't this building-blocks approach really nicer and more pleasant, as well as more productive, than programming in terms of big blobs of mushed-up glue...?-)

这篇关于在python中将列表转换为HTML表格的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆