Python Forum

Full Version: code for CSV file to html file without pandas
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Need to write information into csv and then transfer this information to HTML file.
There are many other libraries PyPi this if don't want to use Pandas eg CSVtoTable
If doing as training i can write a start example of one way it could be done.
Output:
# test.csv Identifier,First name,Last name 901242,Rachel,Booker 207074,Laura,Grey 408129,Craig,Johnson
import csv

with open("test.csv") as csvfile:
    reader = csv.DictReader(csvfile, delimiter=",")
    for row in reader:
        print("<tr>")
        td = '<td>'
        for fn in reader.fieldnames:
            print(f"{td:>6}{row[fn]}</td>")
        print("</tr>")
Output:
<tr> <td>901242</td> <td>Rachel</td> <td>Booker</td> </tr> <tr> <td>207074</td> <td>Laura</td> <td>Grey</td> </tr> <tr> <td>408129</td> <td>Craig</td> <td>Johnson</td> </tr>