cavereaper
cavereaper

Reputation: 41

Write an large array to a text file with specific format (as it is)

I need to write several NxM arrays generated with numpy to ascii files in the following format: tab separated values, row per line, no brackets, the dimension of the array (columns rows) in the header.

I have tried something like this:

import numpy as np

mat = np.random.random((73,92))
dim = mat.shape

header = "{col}\t{row}\n".format(row = dim[0], col = dim[1])
content = str(mat).replace(' [', '').replace('[', '').replace(']', '').replace(' ', '\t')

mat_PM  = open('./output_file.dat', "w+")

mat_PM.write(header)
mat_PM.write(content)

mat_PM.close()

which returns a file with a summarized array, like this: output_file.dat

How do we print the full array?.

Any help is much appreciated.

Upvotes: 0

Views: 924

Answers (2)

Aidan Wansbrough
Aidan Wansbrough

Reputation: 89

I would use Python's str.join() method in order to manually create the content string (instead of using str() on the matrix):

content = "\n".join(["\t".join([str(value) for value in row]) for row in mat])

Or the same technique but without using list comprehensions:

row_strings = []
for row in mat:
    values = []
    for value in row:
        values.append(str(value))
    row_strings.append("\t".join(values))
content = "\n".join(row_strings)

If instead you want to use numpy's built in string method, you can configure numpy to return the whole array (as explained in this answer):

np.set_printoptions(threshold=np.inf, linewidth=np.inf)
content = str(mat).replace(' [', '').replace('[', '').replace(']', '').replace(' ', '\t')

Hope this is useful :)

Upvotes: 1

Michael Delgado
Michael Delgado

Reputation: 15442

Numpy has a function to write arrays to text files with lots of formatting options - see the docs for numpy.savetxt and the user guide intro to saving and loading numpy objects.

In your case, something like the following should work:

with open('./output_file.dat', "w+") as mat_PM:
    mat_PM.write(header)
    np.savetxt(mat_PM, mat, fmt="%9f", delimiter="\t")

Upvotes: 2

Related Questions