Skip to content

Instantly share code, notes, and snippets.

@xuhdev
Last active December 10, 2015 12:58
Show Gist options
  • Save xuhdev/4437648 to your computer and use it in GitHub Desktop.
Save xuhdev/4437648 to your computer and use it in GitHub Desktop.
load matrix from an ascii file 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)
fo.close()
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.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment