Write a python program that opens a csv data file (sampledata.csv) and reads int
ID: 3775461 • Letter: W
Question
Write a python program that opens a csv data file (sampledata.csv) and reads into a list. Then it writes to an HTML page. The program should read data from the input file and merge it into HTML statements, into an output file. The hardcoded data in the html file (sampletags.html) should be replace with the data from the file. Use a loop to write out the divs in the HTML file. Name the output file sampleoutput.html.
Here are the fields in the order the appear in a row of data in the file:
date_of_registration
title
firstname
lastname
address1
address2
city
state
zipcode
telephone
email
website
position
company
meals
billing_firstname
billing_lastname
card_type
card_number
card_csv
exp_year
exp_month
session1
session2
session3
sampledata.csv
sampletags.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<main>
<div class="wrap">
<div class="row1">
<div class="tag">
<div class="firstName">Jack</div>
<div class="lastName">Smith</div>
<div class="title">Sales</div>
<div class="company">Sales Inc.</div>
<div class="address">Salinas, CA</div>
</div>
<div class="tag">
<div class="firstName">Jack</div>
<div class="lastName">Smith</div>
<div class="title">Sales</div>
<div class="company">Sales Inc.</div>
<div class="address">Salinas, CA</div>
</div>
<div class="tag">
<div class="firstName">Jack</div>
<div class="lastName">Smith</div>
<div class="title">Sales</div>
<div class="company">Sales Inc.</div>
<div class="address">Salinas, CA</div>
</div>
</div>
</main>
</body>
</html>
Explanation / Answer
import csv
list=[]
with open('sampledata.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
list1=[]
for i in row:
list1.append(i)
list.append(list1)
f = open('sampletags.html', 'r')
g= open('sampleoutput.html', 'r')
#sampleoutput.html
f.write("""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<main>
<div class="wrap">""")
count=1
for i in list:
f.write("<div"row"="+str(count)+">")
for j in i:
f.write("<div class=/"tag/">")
f.write("div"+str(j)+"</div>")
f.write("</div>")
f.write("</div>")
count=count+1
f.write("""</div>
</main>
</body>
</html>""")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.