I'm new to Python and I have a solution for this but it seems slow and silly, so I wondered if there is a better way?
Say I have a matrix defined like this:
mat = [['hello']*4 for x in xrange(3)]
I am using this function to write it to file:
def writeMat(mat, outfile):
with open(outfile, "w") as f:
for item in mat:
f.writelines(str(item).replace('[','').replace(',','').replace('\'','').replace(']','\n'))
writeMat(mat, "temp.txt")
which gives a text file that looks like:
hello hello hello hello
hello hello hello hello
hello hello hello hello
The files that I am dealing with are very large. The savetxt function in numpy would be great, but I don't want to store this as a numpy array because while the majority of the matrix is comprised of single character elements, the first few columns will be many characters in length, and it seems to me (correct me if I am wrong) this would mean the whole matrix would use much more memory than is necessary because every element in the matrix will be the size of the largest element.