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

后端 未结 7 1181
死守一世寂寞
死守一世寂寞 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: 
    
    
    Column_0 Column_1 Column_2 Column_3
    0 one two three four
    1 five six seven eight
    2 nine

    code_output_with_spanning_along_a_row

提交回复
热议问题