1

I often work with data stored in text files with [x, y, z] like format. When loading the data into a NumPy array, it is convenient to maintain the ordering from the text file, where each column is a different element, x, y, or z. The downside to this is with the C ordering NumPy uses as the default, operating on all the x-values mean accessing non-contiguous memory blocks.

To load the data into a Fortran ordered array, I can use

data = numpy.asfortranarray(numpy.loadtxt('data.txt'))

but is there a way to do that in one line? Looking at the loadtxt documentation, this method does not seem to provide that functionality. Is there an another load function that does?

1
  • Do you realize that loadtxt can allocate memory for a row but not for a column beforehand? So the best it could do is read the array in C order and then convert it to F order. Commented Dec 9, 2016 at 18:34

1 Answer 1

4

Both loadtxt and genfromtxt read the file line by line and collect results in a list of lists (or list of tuples). At the end they convert this to an array. Roughly:

rows = []
for line in f.readline():
    values = [float(i) for i  in line.split(delimiter)]
    rows.append(values)
data = np.array(rows)

You could write your own reader! Or just accept this extra bit of post processing. It doesn't look hard or expensive.

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

1 Comment

That makes sense. Essentially, because the file is stored in rows, to get a column-major array, Fortran ordering, the file has to be read in using row-major ordering, C ordering, then transformed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.