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

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

问题描述

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

lets say I have a list like so:

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



<我希望尝试将这些数据转换为各种维度的HTML表格:

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

one    four   seven
two    five   eight
three  six    nine

one    two   three  four
five   six   seven  eight
nine

是否存在可以处理此问题的库,无需进行疯狂列表拼接或嵌套for循环?我的谷歌搜索显示有一些HTML库,但我没有时间浏览每一个,看看他们是否可以很好地处理表。有没有人不得不这样做?如果是这样你是怎么做到的?

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:


  • 给出一个平面列表,产生一个子列表列表,其中子列表具有给定长度,整个列表可以走进行主要顺序(你的第一个和第三个例子)或专栏(你的第二个例子);

  • 给出一个带有字符串项的子列表列表,从中生成一个HTML表格。

我认为这两个任务真的非常截然不同,并且没有什么可以获得(并且输掉很多),所以如果有的话我会感到惊讶精心设计的图书馆做得如此糊涂。

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.

对于第1点,行主要很容易:

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)]

和专栏也不差:

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

例如......:

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

产生你想要的三个结果(每行一个列表,不是HTML格式; - )。

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

问题的后半部分 - 从字符串列表列表中生成HTML表:

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>'

如果你想把它作为一个单一的stri而不是打印出来,将每个 print 更改为 yield 并使用'\ n '.join(html_table(lol))

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

现在你有两个简单,有用,可用和可重用的构建块 - 让它们分开每当你想要将数据呈现为HTML表格时,以及每当作为HTML表格呈现的列表列表来自任何其他构建它的方式时,它都会派上用场。将它们放在一起很容易在您的应用程序代码中完成,但当然也可以轻松地执行一个简单的粘合例程,例如,假设 yield 基于<的版本 html_table 并且需要一个字符串结果:

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天全站免登陆