Skip to content

Instantly share code, notes, and snippets.

@stucka
Created October 8, 2019 15:35
Show Gist options
  • Save stucka/041ca1c753a628851cfde3dadf85aac3 to your computer and use it in GitHub Desktop.
Save stucka/041ca1c753a628851cfde3dadf85aac3 to your computer and use it in GitHub Desktop.
CSV to HTML converter (terrible)
def csv-to-html(sourcefilename)
# Try a csv-to-HTML converter?
from slugify import slugify
import os
import csv
currentdir = os.path.dirname(".")
# sourcefilename = "scraperreport.csv"
with open(sourcefilename, "r") as f:
reader = list(csv.DictReader(f))
newl = "\r\n"
basefilename = sourcefilename.replace(".csv", "")
html = f"<html><head><title>{currentdir} - {basefilename}</title></head>{newl}<body>{newl}<table>"
html += "<tr>"
for item in list(reader[0].keys()):
html += '<th class="' + slugify(item) + '">' + item + '</th>' + newl
html += "</tr>"
for row in reader:
html += '<tr>'
for item in list(row.keys()):
html += '<td class="' + slugify(item) + '">' + row[item] + "</td>"
html += f"</tr>{newl}"
html += f"</table>{newl}{newl}</body></html>"
with open(f"{basefilename}.html", "w", newline="") as f:
f.write(html)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment