HTML in Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nonemsludo
    New Member
    • Apr 2010
    • 15

    HTML in Python

    I would like to create a column to show the average of two columns, y[12] and y[13] from a txt file I tried it like below
    Code:
    f = open(’weather.txt’, ’r’)
    g = open(’weather.html’, ’w’)
    l = f.readlines()
    g.write("<html>\n<head>\n<title>Weather by city</title>\n</head>\n")
    g.write("<body>\n<table border=’2’><tr><th>City</th>"+
    "<th>Average.</th></tr>\n")
    for x in l:
    y = x.split(",")
    if len(y) > 13:
    g.write("<tr><td>" + y[1] + "</td><td>" +(sum( y[12] , y[13])/2) +
    "</td></tr>\n")
    g.close()
    f.close()
    It doesn't work though
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    It would, in general, be more helpful to give specific error messages and details that "it doesn't work though". Especially since you're not giving weather.txt or even a few rows of weather.txt. Help us to help you!

    But in this particular case it's likely to be that y[12] and y[13] are both strings, rather than numbers. So change line 10 to:

    Code:
    g.write("<tr><td>" + y[1] + "</td><td>" +str(float( y[12])+float(y[13])/2) +"</td></tr>\n")
    This will probably be a bit ugly, so you could try this instead:
    Code:
    g.write("<tr><td>" + y[1] + "</td><td>%.2f</td></tr>\n"%(float( y[12])+float(y[13])/2))

    Comment

    • Nonemsludo
      New Member
      • Apr 2010
      • 15

      #3
      Glenton thanks a bunch, you are right on target,

      Comment

      Working...