Read write matrix


In [1]:
%pylab --no-import-all inline


Populating the interactive namespace from numpy and matplotlib

set up a random matrix


In [2]:
dimension = 10
matrix = np.random.random(dimension * dimension).reshape(dimension, dimension)
print(matrix)


[[ 0.43353106  0.20119405  0.86392037  0.32404592  0.08291246  0.59166994
   0.60206427  0.12748406  0.51412819  0.91065312]
 [ 0.40606504  0.10030935  0.34770477  0.26451174  0.12690505  0.83409554
   0.18351937  0.6606988   0.22986398  0.59035432]
 [ 0.43802659  0.08093542  0.61796533  0.11181067  0.35010534  0.18116558
   0.92906712  0.4880652   0.5330792   0.60887044]
 [ 0.01730932  0.33259982  0.43341601  0.8325671   0.0238194   0.28202742
   0.64386667  0.96173036  0.42287681  0.71983562]
 [ 0.91209496  0.94745346  0.11092897  0.52060484  0.83411446  0.71373436
   0.72706503  0.0597849   0.28280784  0.54419517]
 [ 0.08279205  0.52549851  0.73353343  0.17877879  0.1276122   0.69785561
   0.28603898  0.11544235  0.21754224  0.15313346]
 [ 0.5963676   0.99209628  0.65830609  0.6833042   0.53988082  0.03371555
   0.02230531  0.98675546  0.26948061  0.11284479]
 [ 0.19031027  0.05051354  0.01919686  0.67331308  0.92151899  0.13854699
   0.04121859  0.68428878  0.77745517  0.98338923]
 [ 0.47131067  0.99289866  0.61520688  0.20562814  0.87475133  0.02509654
   0.95875837  0.61730798  0.05002505  0.84926377]
 [ 0.20119436  0.91923225  0.18235957  0.88617741  0.23535837  0.85673974
   0.64305386  0.98699058  0.94663796  0.20793874]]

In [3]:
def print_matrix(matrix, ncol=4):
    dimension = matrix.shape[0]
    j = 0
    out = ""
    while j < dimension:
        jmax = min(dimension, j + ncol)
        out += 5 * " " + " ".join(["%12d" % jj for jj in range(j, jmax)]) + "\n"
        for i in range(dimension):
            out += "%5d" % i 
            out += " ".join(["%12.6f" % matrix[i, jj] for jj in range(j, jmax)]) + "\n"
        j += ncol
    return out

out = print_matrix(matrix)
print(out)


                0            1            2            3
    0    0.433531     0.201194     0.863920     0.324046
    1    0.406065     0.100309     0.347705     0.264512
    2    0.438027     0.080935     0.617965     0.111811
    3    0.017309     0.332600     0.433416     0.832567
    4    0.912095     0.947453     0.110929     0.520605
    5    0.082792     0.525499     0.733533     0.178779
    6    0.596368     0.992096     0.658306     0.683304
    7    0.190310     0.050514     0.019197     0.673313
    8    0.471311     0.992899     0.615207     0.205628
    9    0.201194     0.919232     0.182360     0.886177
                4            5            6            7
    0    0.082912     0.591670     0.602064     0.127484
    1    0.126905     0.834096     0.183519     0.660699
    2    0.350105     0.181166     0.929067     0.488065
    3    0.023819     0.282027     0.643867     0.961730
    4    0.834114     0.713734     0.727065     0.059785
    5    0.127612     0.697856     0.286039     0.115442
    6    0.539881     0.033716     0.022305     0.986755
    7    0.921519     0.138547     0.041219     0.684289
    8    0.874751     0.025097     0.958758     0.617308
    9    0.235358     0.856740     0.643054     0.986991
                8            9
    0    0.514128     0.910653
    1    0.229864     0.590354
    2    0.533079     0.608870
    3    0.422877     0.719836
    4    0.282808     0.544195
    5    0.217542     0.153133
    6    0.269481     0.112845
    7    0.777455     0.983389
    8    0.050025     0.849264
    9    0.946638     0.207939


In [4]:
with open("matrix.dat", "w") as f:
    f.write("A title and blank lines\n\n\n")
    f.write("$matrix\n")
    f.write("%d\n" % dimension)
    f.write("\n")
    f.write(out)

In [5]:
cat matrix.dat


A title and blank lines


$matrix
10

                0            1            2            3
    0    0.433531     0.201194     0.863920     0.324046
    1    0.406065     0.100309     0.347705     0.264512
    2    0.438027     0.080935     0.617965     0.111811
    3    0.017309     0.332600     0.433416     0.832567
    4    0.912095     0.947453     0.110929     0.520605
    5    0.082792     0.525499     0.733533     0.178779
    6    0.596368     0.992096     0.658306     0.683304
    7    0.190310     0.050514     0.019197     0.673313
    8    0.471311     0.992899     0.615207     0.205628
    9    0.201194     0.919232     0.182360     0.886177
                4            5            6            7
    0    0.082912     0.591670     0.602064     0.127484
    1    0.126905     0.834096     0.183519     0.660699
    2    0.350105     0.181166     0.929067     0.488065
    3    0.023819     0.282027     0.643867     0.961730
    4    0.834114     0.713734     0.727065     0.059785
    5    0.127612     0.697856     0.286039     0.115442
    6    0.539881     0.033716     0.022305     0.986755
    7    0.921519     0.138547     0.041219     0.684289
    8    0.874751     0.025097     0.958758     0.617308
    9    0.235358     0.856740     0.643054     0.986991
                8            9
    0    0.514128     0.910653
    1    0.229864     0.590354
    2    0.533079     0.608870
    3    0.422877     0.719836
    4    0.282808     0.544195
    5    0.217542     0.153133
    6    0.269481     0.112845
    7    0.777455     0.983389
    8    0.050025     0.849264
    9    0.946638     0.207939

Read the matrix


In [6]:
with open("matrix.dat", "r") as f:
    for i in range(7):
        line = f.readline()
        print(line.strip())


A title and blank lines


$matrix
10

0            1            2            3

In [7]:
with open("matrix.dat", "r") as f:
    [f.readline() for i in range(3)]
    line = f.readline()
    print(line)


$matrix


In [8]:
def read_matrix(filename):
    with open(filename, "r") as f:
        # read dimension
        dimension = None
        while not dimension:
            line = f.readline()
            if "$matrix" in line:
                dimension = int(f.readline())
        print("dimension :", dimension)
        # allocate matrix
        matrix = np.zeros((dimension, dimension))
        # read the blanck line
        f.readline()
        # read the matrix
        while line != "":
            line = f.readline()
            jindex = [int(jj) for jj in line.split()]
            for i in range(dimension):
                line = f.readline()
                data = [float(val) for val in line.split()[1:]]
                for k, val in enumerate(data):
                    matrix[i, jindex[k]] = val           
    return matrix

rmatrix = read_matrix("matrix.dat")
out = print_matrix(rmatrix)
print(out)


dimension : 10
                0            1            2            3
    0    0.433531     0.201194     0.863920     0.324046
    1    0.406065     0.100309     0.347705     0.264512
    2    0.438027     0.080935     0.617965     0.111811
    3    0.017309     0.332600     0.433416     0.832567
    4    0.912095     0.947453     0.110929     0.520605
    5    0.082792     0.525499     0.733533     0.178779
    6    0.596368     0.992096     0.658306     0.683304
    7    0.190310     0.050514     0.019197     0.673313
    8    0.471311     0.992899     0.615207     0.205628
    9    0.201194     0.919232     0.182360     0.886177
                4            5            6            7
    0    0.082912     0.591670     0.602064     0.127484
    1    0.126905     0.834096     0.183519     0.660699
    2    0.350105     0.181166     0.929067     0.488065
    3    0.023819     0.282027     0.643867     0.961730
    4    0.834114     0.713734     0.727065     0.059785
    5    0.127612     0.697856     0.286039     0.115442
    6    0.539881     0.033716     0.022305     0.986755
    7    0.921519     0.138547     0.041219     0.684289
    8    0.874751     0.025097     0.958758     0.617308
    9    0.235358     0.856740     0.643054     0.986991
                8            9
    0    0.514128     0.910653
    1    0.229864     0.590354
    2    0.533079     0.608870
    3    0.422877     0.719836
    4    0.282808     0.544195
    5    0.217542     0.153133
    6    0.269481     0.112845
    7    0.777455     0.983389
    8    0.050025     0.849264
    9    0.946638     0.207939

next or readline

Try to mix next() or f.readline() method.


In [9]:
with open("matrix.dat", "r") as f:
    line = next(f)
    print("line 1", line.strip())
    
    [next(f) for i in range(5)]

    line = next(f)
    
    print("line 2", line.strip())
    print(10*"------")
    
    i = 0
    for line in f:
        i += 1
        print(line.strip())
        if i == 5:
            break
    
    print(10*"------")
    
    i = 0
    while i < 6:
        i += 1
        line = f.readline()
        print(line.strip())


line 1 A title and blank lines
line 2 0            1            2            3
------------------------------------------------------------
0    0.433531     0.201194     0.863920     0.324046
1    0.406065     0.100309     0.347705     0.264512
2    0.438027     0.080935     0.617965     0.111811
3    0.017309     0.332600     0.433416     0.832567
4    0.912095     0.947453     0.110929     0.520605
------------------------------------------------------------
5    0.082792     0.525499     0.733533     0.178779
6    0.596368     0.992096     0.658306     0.683304
7    0.190310     0.050514     0.019197     0.673313
8    0.471311     0.992899     0.615207     0.205628
9    0.201194     0.919232     0.182360     0.886177
4            5            6            7