Load A Matrix from An ASCII Format File (C++ and Python)

It is common for an scientific program to load an ASCII format matrix file, i.e. an ASCII text file consisting of lines of float numbers separated by whitespaces. In this post, I am gonna show my code (C++ and Python) to load a matrix from an ASCII file.


The following C++ function is to load a matrix from an ASCII file into a vector< vector<double> > object, some kind of "C++ style" 2D array.

#include <istream>
#include <string>
#include <sstream>
#include <vector>

// load matrix from an ascii text file.
void load_matrix(std::istream* is,
        std::vector< std::vector<double> >* matrix,
        const std::string& delim = " \t")
    using namespace std;

    string      line;
    string      strnum;

    // clear first

    // parse line by line
    while (getline(*is, line))

        for (string::const_iterator i = line.begin(); i != line.end(); ++ i)
            // If i is not a delim, then append it to strnum
            if (delim.find(*i) == string::npos)
                strnum += *i;
                if (i + 1 != line.end()) // If it's the last char, do not continue

            // if strnum is still empty, it means the previous char is also a
            // delim (several delims appear together). Ignore this char.
            if (strnum.empty())

            // If we reach here, we got a number. Convert it to double.
            double       number;

            istringstream(strnum) >> number;


// example
#include <fstream>
#include <iostream>

int main()
    using namespace std;

    // read the file
    std::ifstream is("input.txt");

    // load the matrix
    std::vector< std::vector<double> > matrix;
    load_matrix(&is, &matrix);

    // print out the matrix
    cout << "The matrix is:" << endl;
    for (std::vector< std::vector<double> >::const_iterator it = matrix.begin(); it != matrix.end(); ++ it)
        for (std::vector<double>::const_iterator itit = it->begin(); itit != it->end(); ++ itit)
            cout << *itit << '\t';

        cout << endl;

    return 0;

The code is also available on GitHub Gist.


The Python code loads the matrix into a numpy.matrix object.

def load_matrix_from_file(f):
    This function is to load an ascii format matrix (float numbers separated by
    whitespace characters and newlines) into a numpy matrix object.

    f is a file object or a file path.

    import types
    import numpy

    if type(f) == types.StringType:
        fo = open(f, 'r')
        matrix = load_matrix_from_file(fo)
        return matrix
    elif type(f) == types.FileType:
        file_content = f.read().strip()
        file_content = file_content.replace('\r\n', ';')
        file_content = file_content.replace('\n', ';')
        file_content = file_content.replace('\r', ';')

        return numpy.matrix(file_content)

    raise TypeError('f must be a file object or a file name.')

The code is also available on GitHub Gist.

If you want to get a nested list instead of such a numpy.matrix object, you can use the following lines to convert the object to a nested list:

matrix = load_matrix_from_file('file_name')
nested_list = matrix.tolist()