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]:
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]:
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]:
In [9]:
ntrows = make_ntgrid(grid)
In [10]:
ntcols = make_ntgrid(transpose(grid))
In [11]:
ntrows
Out[11]:
In [12]:
ntcols
Out[12]:
In [13]:
ntcols[0]
Out[13]:
In [14]:
ntcols.Energy_Per_Conditioned_Building_Area__kWh_m2_.Total_Source_Energy
Out[14]:
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]:
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))