{"id":2420,"date":"2019-07-22T15:37:53","date_gmt":"2019-07-22T13:37:53","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=2420"},"modified":"2019-07-22T19:34:44","modified_gmt":"2019-07-22T17:34:44","slug":"python-to-make-html-tables-code","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/python-to-make-html-tables-code\/","title":{"rendered":"Python to make html tables code"},"content":{"rendered":"<p>I think that to make a table in html is really boring. So I thought it could be nice to use Python to avoid having to write all those html tags.<\/p>\n<p>The simpliest table in the world would be like this:<\/p>\n<pre class=\"lang:default decode:true\">&lt;table border=1&gt;\r\n&lt;tr&gt;&lt;td&gt;Name&lt;\/td&gt;&lt;td&gt;Surname&lt;\/td&gt;&lt;td&gt;Age&lt;\/td&gt;&lt;\/tr&gt;\r\n&lt;tr&gt;&lt;td&gt;John&lt;\/td&gt;&lt;td&gt;Marr&lt;\/td&gt;&lt;td&gt;18&lt;\/td&gt;&lt;\/tr&gt;\r\n&lt;tr&gt;&lt;td&gt;Marion&lt;\/td&gt;&lt;td&gt;Sally&lt;\/td&gt;&lt;td&gt;28&lt;\/td&gt;&lt;\/tr&gt;\r\n&lt;\/table&gt;<\/pre>\n<p>This is it:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td>Name<\/td>\n<td>Surname<\/td>\n<td>Age<\/td>\n<\/tr>\n<tr>\n<td>John<\/td>\n<td>Marr<\/td>\n<td>18<\/td>\n<\/tr>\n<tr>\n<td>Marion<\/td>\n<td>Sally<\/td>\n<td>28<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Using Python to do the boring stuffs<\/h2>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/gottadothejob.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2443 aligncenter\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/gottadothejob.jpg\" alt=\"\" width=\"425\" height=\"317\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/gottadothejob.jpg 425w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/gottadothejob-320x239.jpg 320w\" sizes=\"auto, (max-width: 425px) 100vw, 425px\" \/><\/a><\/p>\n<p>Imagine having to do more complex tables. So, here is the code to avoid all this.<\/p>\n<p>First, we write the data in this simple way<\/p>\n<pre class=\"lang:default decode:true\">data = \"\"\"\r\n\r\n        Name,    Surname,    Age\r\n        John,    Smith,      18\r\n        Mary,    Luise,      24\r\n             \r\n\"\"\"<\/pre>\n<p>Then we split the lines and delete the empty ones, adding also the tr and some td tags.<\/p>\n<pre class=\"lang:default decode:true\">data = data.splitlines()\r\ndata = [d.strip() for d in data]\r\ndata = [f\"&lt;tr&gt;&lt;td&gt;{d}&lt;\/tr&gt;\" for d in data if d.strip() != \"\"]<\/pre>\n<p>Then we let python add the table tag at the end and the beginnig<\/p>\n<pre class=\"lang:default decode:true\">data = \"&lt;table border=1&gt;\" + \"\".join(data) + \"&lt;\/table&gt;\"<\/pre>\n<p>We replace the commas with something useful (the rest of the tags we need)<\/p>\n<pre class=\"lang:default decode:true \">data = data.replace(\",\",\"&lt;\/td&gt;&lt;td&gt;\")<\/pre>\n<p>And we save our code in an html file, we&#8217;re done<\/p>\n<pre class=\"lang:default decode:true \">with open(\"table.html\", \"w\", encoding=\"utf-8\") as file:\r\n    file.write(data)<\/pre>\n<h3>The final output<\/h3>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/tab1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2441\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/tab1.png\" alt=\"\" width=\"160\" height=\"88\" \/><\/a><\/p>\n<h2>The entire code<\/h2>\n<p>This is the whole code (made in jupyter lab, so you&#8217;ll find something more, watch the video to understand why there are those other lines of code).<\/p>\n<pre class=\"lang:default decode:true \">from IPython.core.interactiveshell import InteractiveShell\r\nInteractiveShell.ast_node_interactivity = \"all\"\r\nfrom IPython.display import display, HTML\r\n\r\nimport os\r\n\r\ndata = \"\"\"\r\n\r\n        Name,    Surname,    Age\r\n        John,    Smith,      18\r\n        Mary,    Luise,      24\r\n             \r\n\"\"\"\r\ndata = data.splitlines()\r\ndata = [d.strip() for d in data]\r\ndata = [f\"&lt;tr&gt;&lt;td&gt;{d}&lt;\/tr&gt;\" for d in data if d.strip() != \"\"]\r\ndata = \"&lt;table border=1&gt;\" + \"\".join(data) + \"&lt;\/table&gt;\"\r\n#display(HTML(data))\r\ndata = data.replace(\"    \",\"\")\r\ndata = data.replace(\",\",\"&lt;\/td&gt;&lt;td&gt;\")\r\ndata\r\n# Comment this if you don't use jupyter\r\ndisplay(HTML(data))\r\nwith open(\"table.html\", \"w\", encoding=\"utf-8\") as file:\r\n    file.write(data)\r\nos.startfile(\"table.html\")<\/pre>\n<p>The video about making an html table with Python<\/p>\n<p><iframe loading=\"lazy\" title=\"Python to make html tables\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/ASglN4Dg1wU?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h2><\/h2>\n<p>The code without the jupyter lab commands<\/p>\n<pre class=\"lang:default decode:true \">import os\r\n\r\ndata = \"\"\"\r\n\r\n        Name,    Surname,    Age\r\n        John,    Smith,      18\r\n        Mary,    Luise,      24\r\n             \r\n\"\"\"\r\ndata = data.splitlines()\r\ndata = [d.strip() for d in data]\r\ndata = [f\"&lt;tr&gt;&lt;td&gt;{d}&lt;\/tr&gt;\" for d in data if d.strip() != \"\"]\r\ndata = \"&lt;table border=1&gt;\" + \"\".join(data) + \"&lt;\/table&gt;\"\r\ndata = data.replace(\"    \",\"\")\r\ndata = data.replace(\",\",\"&lt;\/td&gt;&lt;td&gt;\")\r\nwith open(\"table.html\", \"w\", encoding=\"utf-8\") as file:\r\n    file.write(data)\r\nos.startfile(\"table.html\")<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2>Another way to do it<\/h2>\n<p>This code was a little different, but the output is the same.<\/p>\n<pre class=\"lang:default decode:true \">import os\r\n\r\n\r\n\r\ndef savehtml(filename=\"defaulthtml\"):\r\n    htmldata = data2list(data)\r\n    htmldata = htmldata.replace(\"&lt;table&gt;\", f\"&lt;table border={border}\")\r\n    with open(\"Simple.html\", \"w\", encoding=\"utf-8\") as filehtml:\r\n        filehtml.write(htmldata)\r\n    os.startfile(\"Simple.html\")\r\n\r\ndef data2list(data):\r\n    d = data.splitlines()[1:-1]\r\n    d = [x.split(\",\") for x in d]\r\n    for row in d:\r\n        for e in row:\r\n            e_index = row.index(e)\r\n            cell = \"&lt;td&gt;\" + e.strip() + \"&lt;\/td&gt;\"\r\n            if e_index == 0:\r\n                d[d.index(row)][row.index(e)] = \"&lt;tr&gt;\" + cell\r\n            elif e_index == len(row) -1:\r\n                d[d.index(row)][row.index(e)] = cell + \"&lt;\/tr&gt;\"\r\n            else:\r\n                d[d.index(row)][e_index] = cell\r\n    d = [i for sublist in d for i in sublist]\r\n    return \"&lt;table&gt;\" + \"\".join(d) + \"&lt;\/table&gt;\"\r\n \r\n \r\n# ============== Persolized data ================\r\nborder = 1\r\ndata = \"\"\"\r\nImpiegato,      Performance,    data\r\nRossi Mario,    1000,           1\/2\/2018\r\nBaldo Franco,   2000,           1\/2\/2018\r\n    \"\"\"\r\nsavehtml(filename=\"Simple.html\")<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2438 aligncenter\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/output.png\" alt=\"\" width=\"563\" height=\"355\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/output.png 563w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/output-320x202.png 320w\" sizes=\"auto, (max-width: 563px) 100vw, 563px\" \/><\/a><\/p>\n<h2>Other posts about the same topic<\/h2>\n<ul>\n<li><a href=\"https:\/\/pythonprogramming.altervista.org\/1563-2\/\">Create a table in html, but with Python<\/a><\/li>\n<li><a href=\"https:\/\/pythonprogramming.altervista.org\/creare-una-tabella-html-con-python\/\">Create an html table with Python<\/a><\/li>\n<li><a href=\"https:\/\/pythonprogramming.altervista.org\/fastest-way-to-use-excel-with-python-to-make-a-table\/\">Fastest way to create a table<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"Make a table in html without having to write a single tag, using this python code and saving a lot of time\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/python-to-make-html-tables-code\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":2421,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,370],"tags":[321,455,89,4,320],"class_list":["post-2420","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","category-html","tag-html","tag-jupiter-lab","tag-jupyter","tag-python","tag-table"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=2420"}],"version-history":[{"count":12,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2420\/revisions"}],"predecessor-version":[{"id":2444,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2420\/revisions\/2444"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/2421"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=2420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=2420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=2420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}