'How to reshape a text file in Python so that the rows/columns swap position?

EDIT - SOLVED

I have a text file which contains 100 rows of data, each with 500 columns of values. I need to simply swap these so that my file contains 500 rows of data and 100 columns of values.

Everything in the first column [:,0] will be row #1, everything in the second column [:,1] will be row #2 - and so on until the end of the file.

I have searched for solutions and stumbled across np.reshape, but for the life of me I haven't been able to find an example to use on an existing text file. I should mention I'm also not overly skilled with Python to figure this out on my own.


Alternatively, here is the code I used to create this text file in the first place - if it is simpler to just fix something in here which will reshape it in the first place then I am open for suggestions.

diffs = []
for number in range (1,101):
    filea = pl.loadtxt('file' + str(number) + 'a')
    fileb = pl.loadtxt('file' + str(number) + 'b')
    diff = fileb[:,1] - filea[:,1]
    diffs.append(diff)
    np.savetxt('diffs.txt', (diffs))

Here, I have 100 a files and 100 b files. They each contain 500 rows and 2 columns. I am finding the difference between the values in the second column for each and looking to get them all in a file which maintains the 500 rows, but instead has 100 columns with the diff for 1b-1a 2b-2a etc through to 100b-100a.

Hopefully I have explained myself in a way which can be understood. Thanks in advance for any help.


SOLUTION:

reshape = np.loadtxt('diffs.txt')
diffs2 = np.transpose(reshape)
np.savetxt('diffs2.txt', (diffs2))


Solution 1:[1]

If a more general solution is desired (and the data is stored in files not in Python memory), I have recently written a command line utility that may be useful. Compiled binaries may become available soon, for now a Nim compier is required to build it. I wrote this to learn some Nim, and it may not be optimally performant.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 adigitoleo