Commit f84b1a58 authored by Andreas Fischer's avatar Andreas Fischer
Browse files

Added program exercise and HTML template

parents
#!/usr/bin/env python3
import csv
def read_html():
"""
Read the HTML template file into a string
"""
with open("template.html") as htmlfile:
htmlstring = htmlfile.read()
return htmlstring
def read_csv(filename):
"""
Read a csv file and produce a list of lists with each
inner list representing a row in the csv file:
[ [first, last, email], [first, last, email], ...]
"""
result = []
with open(filename) as csvfile:
### EDIT BELOW HERE ###
reader = csv.reader(csvfile, delimiter=";")
pass
### EDIT ABOVE HERE ###
return result
def csv_to_html(csvdata):
"""
Expects csvdata to be a list of lists:
[ [first, last, email], [first, last, email], ...]
Returns a string with the HTML <tbody> contents for
each row in the csv file:
<tr><td>First name</td><td>Last name</td><td>E-Mail</td></tr>"
"""
htmlstring = ""
### EDIT BELOW HERE ###
htmlline = "<tr><td>{}</td><td>{}</td><td>{}</td></tr>\n"
pass
### EDIT ABOVE HERE ###
return htmlstring
def combine_template_with_data(template_string, htmldata):
"""
Combine the HTML template string with the generated
tabular data.
"""
### EDIT BELOW HERE ###
return ""
### EDIT ABOVE HERE ###
def write_html(htmlstring):
"""
Write the resulting HTML file
"""
with open("result.html", "w") as htmlfile:
htmlfile.write(htmlstring)
def main():
"""
This is the main function of the program
"""
template_string = read_html()
csvdata = read_csv("test.csv")
htmldata = csv_to_html(csvdata)
fullstring = combine_template_with_data(template_string, htmldata)
write_html(fullstring)
if __name__ == "__main__":
main()
<!DOCTYPE html>
<html lang="en">
<head>
<title>My formatted table</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="text-center">My formatted table</h1>
<hr />
<table class="table">
<thead>
<tr><th>First name</th><th>Last name</th><th>E-Mail</th></tr>
</thead>
<tbody>
{}
</tbody>
</table>
<hr />
</div>
</body>
</html>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment