In [1]:
f = open('matrix.txt')
matrix = []
for line in f.readlines():
row = [int(x) for x in line.split(',')]
matrix.append(row)
f.close()
In [2]:
matrix
Out[2]:
In [3]:
f = open('matrix.txt', 'r+')
orig = f.read()
f.seek(0)
#f.write('0,0,0,0\n')
#f.write(orig)
#f.write('\n1,1,1,1')
f.close()
In [4]:
matrix
Out[4]:
In [5]:
matrix = []
with open('matrix.txt') as f:
for line in f.readlines():
row = [int(x) for x in line.split(',')]
matrix.append(row)
matrix
Out[5]:
In [6]:
import os
import numpy as np
if os.path.isfile('ch10.h5'):
os.remove('ch10.h5')
In [7]:
import tables as tb
f = tb.open_file('ch10.h5', 'a')
In [8]:
f.create_group('/', 'a_group', "My Group")
f.root.a_group
Out[8]:
In [9]:
# integer array
f.create_array('/a_group', 'arthur_count', [1, 2, 5, 3])
# tables need descriptions
dt = np.dtype([('id', int), ('name', 'S10')])
knights = np.array([(42, 'Lancelot'), (12, 'Bedivere')], dtype=dt)
f.create_table('/', 'knights', dt)
f.root.knights.append(knights)
In [10]:
f.root.a_group.arthur_count[:]
Out[10]:
In [11]:
type(f.root.a_group.arthur_count[:])
Out[11]:
In [12]:
type(f.root.a_group.arthur_count)
Out[12]:
In [13]:
f.root.knights[1]
Out[13]:
In [14]:
f.root.knights[:1]
Out[14]:
In [15]:
mask = (f.root.knights.cols.id[:] < 28)
f.root.knights[mask]
Out[15]:
In [16]:
f.root.knights[([1, 0],)]
Out[16]:
In [17]:
# don't forget to close the file
f.close()
In [18]:
# clean-up
if os.path.isfile('ch10-1.h5'):
os.remove('ch10-1.h5')
# open a new file
shape = (10, 10000)
f = tb.open_file('ch10-1.h5', "w")
# create the arrays
a = f.create_carray(f.root, 'a', tb.Float32Atom(dflt=1.), shape)
b = f.create_carray(f.root, 'b', tb.Float32Atom(dflt=2.), shape)
c = f.create_carray(f.root, 'c', tb.Float32Atom(dflt=3.), shape)
# evaluate the expression, using the c array as the output
expr = tb.Expr("42*a + 28*b + 6")
expr.set_output(c)
expr.eval()
Out[18]:
In [19]:
# close the file
f.close()