Skip to content

init-zhang/csv-html-table

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

CSV HTML Table

CSV to HTML table converter

I love Markdown but its tables use a lot of characters and takes time to make. I wanted something that still looks like a structured table but takes a fraction of the time to write.

To fix this issue, I wrote a CSV to HTML table converter that can convert CSV within my Markdown files into HTML tables.

Features

  • Commas are used as the delimiter, to include a literal comma within a cell, use a backslash to escape it \,
    • Escaped commas \, are converted back into normal commas
  • There can be any number of leading or trailing spaces around the commas, it's all trimmed during conversion
  • Empty lines between the rows are ignored
  • First row uses table header <th> tags, unless the CSV only has 1 row, otherwise it uses table data <td> tags
  • Supports multiple CSV tables within the same text

Usage

Wrap your CSV data with <table> and </table> tags on their own lines:

<table>
Col 1  , Col 2
Cell 1 , Cell 2
</table>

And call the csv_to_table function from the library with the string containing the CSV:

from csv_to_table import csv_to_table

output = csv_to_table(text)
# "<table><tr><th>Col 1</th><th>Col 2</th></tr><tr><td>Cell 1</td><td>Cell 2</td></tr></table>"

Examples

<table>
Name      , Age , Location       
Alice     , 30  , New York       
Bob       , 25  , Los Angeles    
Charlie   , 35  , San Francisco
</table>
NameAgeLocation
Alice30New York
Bob25Los Angeles
Charlie35San Francisco
<table>
Product    , Price , Stock  

Laptop     , 999   , 10     
Smartphone , 499   , 25
</table>  
ProductPriceStock
Laptop99910
Smartphone49925

About

๐Ÿ—ƒ๏ธ CSV to HTML table converter

Topics

Resources

Stars

Watchers

Forks

Languages