Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code keeps saving the document even though output is identical to original output?

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?

like image 333
Dennis Sylvian Avatar asked Nov 24 '22 11:11

Dennis Sylvian


1 Answers

It's because you're opening the file and then comparing and because it's the same it saves nothing in the file.

Open the file after comparing for example your bottom section of code:

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()

with open('random.csv') as ifile:
    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)

if old_html != html_data:
    if html_data:
        with open('testoutput.html', "wb") as ofile:
            ofile.write(html_data)

Hope this helps :)
- Hyflex

like image 83
Ryflex Avatar answered May 11 '23 00:05

Ryflex