Easiest way to turn a list into an HTML table in python?

后端 未结 7 1165
死守一世寂寞
死守一世寂寞 2020-12-05 00:40

lets say I have a list like so:

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

and I want to experiment

相关标签:
7条回答
  • 2020-12-05 01:37

    Although this has been answered before, here is another solution by using numpy and pandas DataFrame. Since a lot of people are interested in data-science nowadays, I thought solving this using pandas would be fun:

    GITHUB SOLUTION:
    I have made my solution available at my GitHub Repository which you can also run and explore in Google Colaboratory (I strongly recommend this).

    The custom function (generate_html_with_table()) that I used here is available in this Jupyter Notebook.

    SOLUTION:
    To get your solution run the following:

    data = ['one','two','three','four','five','six','seven','eight','nine']
    columns = 4                   # Number of Columns
    columns_or_rows = columns
    column_name_prefix = 'Column' # Prefix for Column headers
    span_axis = 1                 # Span along a row (1) or a column (0) first
    showOutput = True             # Use False to suppress printing output
    
    # Generate HTML
    data_html, data_df = generate_html_with_table(data, columns_or_rows, column_name_prefix, span_axis, showOutput)
    

    OUTPUT:

    HTML Generated: 
    
    <table border="1" class="dataframe">
      <thead>
        <tr style="text-align: right;">
          <th></th>
          <th>Column_0</th>
          <th>Column_1</th>
          <th>Column_2</th>
          <th>Column_3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th>0</th>
          <td>one</td>
          <td>two</td>
          <td>three</td>
          <td>four</td>
        </tr>
        <tr>
          <th>1</th>
          <td>five</td>
          <td>six</td>
          <td>seven</td>
          <td>eight</td>
        </tr>
        <tr>
          <th>2</th>
          <td>nine</td>
          <td></td>
          <td></td>
          <td></td>
        </tr>
      </tbody>
    </table>
    

    code_output_with_spanning_along_a_row

    0 讨论(0)
提交回复
热议问题