In [1]:
import collections
import string

from bs4 import BeautifulSoup, NavigableString, Tag
fname = "resources/outputfiles/V_7_2/5ZoneCAVtoVAVWarmestTempFlowTable_ABUPS.html"
soup = BeautifulSoup(open(fname, 'r'))

In [2]:
import readhtml

In [3]:
def asciidigits(s):
    if s not in string.ascii_letters + string.digits:
        s = '_'
    return s

def nospace(s):
    return ''.join([asciidigits(i) for i in s])

def transpose(arr):
    return map(list, zip(*arr))

In [4]:
lt = readhtml.lines_table(open(fname, 'r'))

In [5]:
grid = lt[0][1]

In [6]:
grid


Out[6]:
[['',
  u'Total Energy [kWh]',
  u'Energy Per Total Building Area [kWh/m2]',
  u'Energy Per Conditioned Building Area [kWh/m2]'],
 [u'Total Site Energy', 47694.47, 51.44, 51.44],
 [u'Net Site Energy', 47694.47, 51.44, 51.44],
 [u'Total Source Energy', 140159.1, 151.16, 151.16],
 [u'Net Source Energy', 140159.1, 151.16, 151.16]]

In [53]:
hnames = grid[0]

In [54]:
hnames = [nospace(n) for n in hnames[1:]]

In [55]:
vnames = [nospace(row[0]) for row in grid[1:]]

In [56]:
hnames


Out[56]:
[u'Total_Energy__kWh_',
 u'Energy_Per_Total_Building_Area__kWh_m2_',
 u'Energy_Per_Conditioned_Building_Area__kWh_m2_']

In [103]:
vnames_s = " ".join(vnames)
ntcol = collections.namedtuple('ntcol', vnames_s)

In [100]:
hnames_s = " ".join(hnames)
ntrow = collections.namedtuple('ntrow', hnames_s)

In [7]:
def make_ntgrid(grid):
    hnames = [nospace(n) for n in grid[0][1:]]
    vnames = [nospace(row[0]) for row in grid[1:]]
    vnames_s = " ".join(vnames)
    hnames_s = " ".join(hnames)
    ntcol = collections.namedtuple('ntcol', vnames_s)
    ntrow = collections.namedtuple('ntrow', hnames_s)
    rdict = [dict(zip(hnames, row[1:])) for row in grid[1:]]  
    ntrows = [ntrow(**rdict[i]) for i, name in enumerate(vnames)]
    ntcols = ntcol(**dict(zip(vnames, ntrows)))
    return ntcols

In [148]:
cdict =  [dict(zip(hnames, row[1:])) for row in grid[1:]]

In [8]:
grid


Out[8]:
[['',
  u'Total Energy [kWh]',
  u'Energy Per Total Building Area [kWh/m2]',
  u'Energy Per Conditioned Building Area [kWh/m2]'],
 [u'Total Site Energy', 47694.47, 51.44, 51.44],
 [u'Net Site Energy', 47694.47, 51.44, 51.44],
 [u'Total Source Energy', 140159.1, 151.16, 151.16],
 [u'Net Source Energy', 140159.1, 151.16, 151.16]]

In [9]:
ntrows = make_ntgrid(grid)

In [10]:
ntcols = make_ntgrid(transpose(grid))

In [11]:
ntrows


Out[11]:
ntcol(Total_Site_Energy=ntrow(Total_Energy__kWh_=47694.47, Energy_Per_Total_Building_Area__kWh_m2_=51.44, Energy_Per_Conditioned_Building_Area__kWh_m2_=51.44), Net_Site_Energy=ntrow(Total_Energy__kWh_=47694.47, Energy_Per_Total_Building_Area__kWh_m2_=51.44, Energy_Per_Conditioned_Building_Area__kWh_m2_=51.44), Total_Source_Energy=ntrow(Total_Energy__kWh_=140159.1, Energy_Per_Total_Building_Area__kWh_m2_=151.16, Energy_Per_Conditioned_Building_Area__kWh_m2_=151.16), Net_Source_Energy=ntrow(Total_Energy__kWh_=140159.1, Energy_Per_Total_Building_Area__kWh_m2_=151.16, Energy_Per_Conditioned_Building_Area__kWh_m2_=151.16))

In [12]:
ntcols


Out[12]:
ntcol(Total_Energy__kWh_=ntrow(Total_Site_Energy=47694.47, Net_Site_Energy=47694.47, Total_Source_Energy=140159.1, Net_Source_Energy=140159.1), Energy_Per_Total_Building_Area__kWh_m2_=ntrow(Total_Site_Energy=51.44, Net_Site_Energy=51.44, Total_Source_Energy=151.16, Net_Source_Energy=151.16), Energy_Per_Conditioned_Building_Area__kWh_m2_=ntrow(Total_Site_Energy=51.44, Net_Site_Energy=51.44, Total_Source_Energy=151.16, Net_Source_Energy=151.16))

In [13]:
ntcols[0]


Out[13]:
ntrow(Total_Site_Energy=47694.47, Net_Site_Energy=47694.47, Total_Source_Energy=140159.1, Net_Source_Energy=140159.1)

In [14]:
ntcols.Energy_Per_Conditioned_Building_Area__kWh_m2_.Total_Source_Energy


Out[14]:
151.16

In [15]:
g = [["",  "a b", "b c", "c d"],
         ["x y", 1,     2,     3 ],
         ["y z", 4,     5,     6 ],
         ["z z", 7,     8,     9 ],]

In [16]:
make_ntgrid(g)


Out[16]:
ntcol(x_y=ntrow(a_b=1, b_c=2, c_d=3), y_z=ntrow(a_b=4, b_c=5, c_d=6), z_z=ntrow(a_b=7, b_c=8, c_d=9))

In [ ]:
ntcol(x_y=ntrow(a_b=1, b_c=2, c_d=3), 
      y_z=ntrow(a_b=4, b_c=5, c_d=6), 
      z_z=ntrow(a_b=7, b_c=8, c_d=9))