Created
October 8, 2019 15:35
-
-
Save stucka/041ca1c753a628851cfde3dadf85aac3 to your computer and use it in GitHub Desktop.
CSV to HTML converter (terrible)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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