In my code below, for some reason it keeps writing the output data to file even though my output data is identical to the newly produced data...
I'm trying to make it so that it only saves when it's different, in order to recreate my problem run the script at least three times and it shouldn't be printing again
more than once
Code example
import csv
def get_html_table(data):
s = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
</head>
<body>
<table id="gradient-style">
<tbody>\n"""
for row in data:
s += ' <tr>'
for counter, cell in enumerate(row):
s += r'<td>{}</td>'.format(cell)
s += '</tr>\n'
s += """ </tbody>
</table>
</body>
</html>"""
return s
with open('testoutput.html', 'rb') as old_html:
old_html = old_html.read()
identifyer = ""
with open('random.csv') as ifile, open('testoutput.html', 'wb') as ofile:
data = []
for counter, row in enumerate(csv.reader(ifile)):
if counter != 0:
datatoapp = [row[0], row[1], row[2], row[3]]
data.append(datatoapp)
html_data = get_html_table(data)
old_html = old_html.strip()
html_data = html_data.strip()
if old_html != html_data.strip():
if html_data:
print "again"
ofile.write(html_data)
print "done"
As you can see the above code turns the csv into a form of html, without some data included.
random.csv
data,data,data,data,data
data,data,data,data,data
data,data,data,data,data
data,data,data,data,data
data,data,data,data,data
Any ideas how to fix this problem?