5

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?

4

2 回答 2

1

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

于 2013-11-06T02:59:22.573 回答
-1

The following reads the entire file, whereas you probably intended to read a single line:

old_html = old_html.read()

P.S. I personally find this sort of reuse of variable names highly confusing.

于 2013-11-05T17:40:49.617 回答