Search code examples
pythonfilematrixpython-itertools

Python: Reading matrices from file as float


I have a very large file (~20 GB) from which I want to read specific lines which represent a matrix. The data file for three 2 x 2 matrices looks like:

2 3
1 3
2 2
1 2
3 2
3 4

Currently I am using the following approach (from here) where I get a list of strings.

import itertools
import matplotlib.pyplot as plt

n = 2 # matrix size
t = 3 # number of matrices
file = open("data")
t = 0;
with file as f:
    while t < 3:
        t=t+1
        next_n_lines = list(islice(f, n))
        print(next_n_lines)
        plt.matshow(next_n_lines)
        plt.show()
        if not next_n_lines:
            break
        # process next_n_lines

But how do I get floats instead of a list of strings? I don't see it, but it can't be so hard.


Solution

  • Extended solution:

    import matplotlib.pyplot as plt, itertools
    
    n = 2
    num_m = 3
    with open('data', 'r') as f:
        for i in range(num_m):
            try:
                items = [list(map(float, i.split())) for i in itertools.islice(f, n)]
            except:
                raise
            else:
                plt.matshow(items)
                plt.show()
    

    The output:

    enter image description here enter image description here enter image description here