How to import a Matrix from a text file in Python

17,182

Try numpy.loadtxt('dmatrix.txt'); you could add a delimiter argument if the file is comma-separated or something.

numpy.load is for files in numpy/python binary formats - created by numpy.save, numpy.savez, or pickle.

Share:
17,182
Sean
Author by

Sean

Updated on June 05, 2022

Comments

  • Sean
    Sean almost 2 years

    Hey this might be a simple question, but I have never had to import any files into Python before.

    So I have a numpy Matrix in a text file, named dmatrix.txt, so how would I be able to import the file into Python and use the matrix?

    I am trying to use numpy.load(), but I am unsure how to use it.

  • Sean
    Sean over 11 years
    Is there a specific way to call the text file? I am getting an error: Traceback (most recent call last): np.loadtxt('dmatrix.txt') fh = iter(open(fname, 'U')) IOError: [Errno 2] No such file or directory: 'dmatrix.txt'
  • Danica
    Danica over 11 years
    Give a relative path from python's working directory, i.e. the directory you're running python from, which you could see by print os.getcwd().