【问题标题】:How to change list into HTML table ? (Python) [duplicate]如何将列表更改为 HTML 表格? (Python)[重复]
【发布时间】:2013-05-07 07:55:48
【问题描述】:

这是我在没有HTML代码时所做的

from collections import defaultdict

hello = ["hello","hi","hello","hello"]
def test(string):
    bye = defaultdict(int)
    for i in hello:
        bye[i]+=1
    return bye

我想将其更改为 html 表,这是我迄今为止尝试过的,但它仍然不起作用

 def test2(string):
    bye= defaultdict(int)
    print"<table>"
    for i in hello:
        print "<tr>"
        print "<td>"+bye[i]= bye[i] +1+"</td>"
        print "</tr>"
    print"</table>"
    return bye

【问题讨论】:

  • 还是不行。>回溯(最近一次调用最后一次): print ""+bye[i]= bye[i] +1+"" ^ SyntaxError:语法无效
  • 你期望的输出是什么?
  • @AshwiniChaudhary 我想要与第一个相同的输出,但添加了 HTML 代码
  • 你为什么在函数完成之前返回?
  • @ErikaSawajiri 第一个和第二个是有区别的,你没有在第一个打印任何东西。发布预期的 html 输出以明确问题。

标签: python html


【解决方案1】:
from collections import defaultdict

hello = ["hello","hi","hello","hello"]

def test2(strList):
  d = defaultdict(int)
  for k in strList:
    d[k] += 1
  print('<table>')
  for i in d.items():
    print('<tr><td>{0[0]}</td><td>{0[1]}</td></tr>'.format(i))
  print('</table>')

test2(hello)

输出

<table>
  <tr><td>hi</td><td>1</td></tr>
  <tr><td>hello</td><td>3</td></tr>
</table>

【讨论】:

    【解决方案2】:

    您可以使用collections.Counter 计算列表中的出现次数,然后使用此信息创建 html 表。 试试这个:

    from collections import Counter, defaultdict
    
    hello = ["hello","hi","hello","hello"]
    counter= Counter(hello)
    bye = defaultdict(int)
    print"<table>"
    for word in counter.keys():
        print "<tr>"
        print "<td>" + str(word) + ":" + str(counter[word]) + "</td>"
        print "</tr>"
        bye[word] = counter[word]
    print"</table>"
    

    此代码的输出将是(您可以根据需要更改格式):

    >>> <table>
    >>> <tr>
    >>> <td>hi:1</td>
    >>> </tr>
    >>> <tr>
    >>> <td>hello:3</td>
    >>> </tr>
    >>> </table>
    

    希望对您有所帮助!

    【讨论】:

      【解决方案3】:

      您不能在打印语句的中间分配变量。您也不能在打印语句中连接字符串类型和整数类型。

      print "<td>"+bye[i]= bye[i] +1+"</td>"
      

      应该是

      bye[i] = bye[i] + 1
      print "<td>"
      print bye[i]
      print '</td>'
      

      而您的 return 语句位于最后的 print 之前,因此它永远不会打印。

      功能齐全

      def test2(string):
          bye= defaultdict(int)
          print"<table>"
          for i in hello:
              print "<tr>"
              bye[i] = bye[i] + 1
              print "<td>"
              print bye[i]
              print '</td>'
              print "</tr>"
              print"</table>"
              return bye
      

      这将是您代码的准确有效翻译,但我不确定您为什么要这样做。 bye 在这里毫无意义,因为您每次都只是打印 1

      【讨论】:

        【解决方案4】:

        Python collections 模块包含Counter 函数,它完全可以做到,需要什么:

        >>> from collections import Counter
        >>> hello = ["hello", "hi", "hello", "hello"]
        >>> print Counter(hello)
        Counter({'hello': 3, 'hi': 1})
        

        现在,您要生成 html。更好的方法是为此使用现有的库。例如Jinja2。你只需要安装它,例如使用pip:

        pip install Jinja2
        

        现在,代码将是:

        from jinja2 import Template
        from collections import Counter
        
        hello = ["hello", "hi", "hello", "hello"]
        
        template = Template("""
        <table>
            {% for item, count in bye.items() %}
                 <tr><td>{{item}}</td><td>{{count}}</td></tr>
            {% endfor %}
        </table>
        """)
        
        print template.render(bye=Counter(hello))
        

        【讨论】:

          猜你喜欢
          • 2011-10-29
          • 2019-02-01
          • 2018-07-06
          • 2021-03-11
          • 1970-01-01
          • 1970-01-01
          • 2022-01-11
          • 1970-01-01
          • 2021-12-26
          相关资源
          最近更新 更多