Python:从文件中读取矩阵为float

时间:2017-10-30 08:30:50

标签: python file matrix itertools

我有一个非常大的文件(~20 GB),我想从中读取代表矩阵的特定行。三个2 x 2矩阵的数据文件如下所示:

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

目前我正在使用以下方法(来自here),其中我得到了一个字符串列表。

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

但是如何获得浮点数而不是字符串列表呢?我没有看到它,但它不会那么难。

3 个答案:

答案 0 :(得分:2)

只需.split行并使用列表推导将float函数映射到结果上,但无论您想要什么:

In [29]: from itertools import *
    ...: n = 2 # matrix size
    ...: t = 3 # number of matrices
    ...: with open('data') as f:
    ...:     for _ in range(t):
    ...:         s = islice(f, n)
    ...:         M = [[float(x) for x in line.split()] for line in s]
    ...:         print(M)
    ...:
[[2.0, 3.0], [1.0, 3.0]]
[[2.0, 2.0], [1.0, 2.0]]
[[3.0, 2.0], [3.0, 4.0]]

另请注意,使用for循环而不是while循环会更清晰。

答案 1 :(得分:1)

扩展解决方案:

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()

输出:

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

答案 2 :(得分:1)

NumPy的fromfile在这里很有用:

import numpy as np

n = 2 # matrix size
t = 3 # number of matrices

with open('data') as fobj:
    for _ in range(t):
        try:
            numbers = np.fromfile(fobj, count=n * n, sep=' ').reshape(n, n)
            plt.matshow(numbers)
            plt.show()
        except ValueError:
            break

产生所需的输出:

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