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]:
[[1, 4, 15, 9], [0, 11, 7, 3], [2, 8, 12, 13], [14, 5, 10, 6]]

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]:
[[1, 4, 15, 9], [0, 11, 7, 3], [2, 8, 12, 13], [14, 5, 10, 6]]

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]:
[[1, 4, 15, 9], [0, 11, 7, 3], [2, 8, 12, 13], [14, 5, 10, 6]]

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]:
/a_group (Group) 'My Group'
  children := []

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]:
[1, 2, 5, 3]

In [11]:
type(f.root.a_group.arthur_count[:])


Out[11]:
list

In [12]:
type(f.root.a_group.arthur_count)


Out[12]:
tables.array.Array

In [13]:
f.root.knights[1]


Out[13]:
(12, b'Bedivere')

In [14]:
f.root.knights[:1]


Out[14]:
array([(42, b'Lancelot')], 
      dtype=[('id', '<i8'), ('name', 'S10')])

In [15]:
mask = (f.root.knights.cols.id[:] < 28)
f.root.knights[mask]


Out[15]:
array([(12, b'Bedivere')], 
      dtype=[('id', '<i8'), ('name', 'S10')])

In [16]:
f.root.knights[([1, 0],)]


Out[16]:
array([(12, b'Bedivere'), (42, b'Lancelot')], 
      dtype=[('id', '<i8'), ('name', 'S10')])

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]:
/c (CArray(10, 10000)) ''
  atom := Float32Atom(shape=(), dflt=3.0)
  maindim := 0
  flavor := 'numpy'
  byteorder := 'little'
  chunkshape := (1, 10000)

In [19]:
# close the file
f.close()