0

I am having trouble to read a matrix from a text file to a numpy array very fast without any unnecessary copying.

The format is the following:

3   
1   -1  6   5   9   7   0   -7  
0   -5  -5  3   -7  -2  4   3   
-2  -9  -5  6   6   -2  -9  9   
-7  -3  3   -3  3   8   8   -8  
9   3   5   2   -2  4   6   5   
-1  -8  7   -4  -1  3   7   4   
0   -4  -8  -3  2   6   2   -2  
-1  8   6   5   9   8   0   6

The first line (3) indicates the size of the matrix i.e here it is 2^3 = 8x8 matrix

Each column is separated with a \t character and each line is separated with a \n character.

I have tried numpy.loadtxt(), I can specify the row delimiter, skip the first row (the 3) but then I need to specify a column delimiter. Can anyone help please?

3
  • 1
    np.loadtxt(<path-to-file>, skiprows=1) should work, what is it giving you? Commented Oct 11, 2017 at 0:03
  • docs.scipy.org/doc/numpy-1.13.0/reference/generated/… Read the docs. Commented Oct 11, 2017 at 0:08
  • In loadtxt, delimiter splits columns. You can't specify a row delimiter. That should be a straight forward load. Commented Oct 11, 2017 at 0:30

1 Answer 1

1

@juanpa the loadtxt(path, skiprows=1) works perfectly, thank you so much!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.