How to export a Numpy array to Comsol?

154 Views Asked by At

I have a Python script that creates a numpy array. How should I save this Numpy array to disk so that it can be read by Comsol?

1

There are 1 best solutions below

2
Felix On

It depends on the format your software needs. If you just want them in one line concatenated by commas, you do it like this:

your_array = [1, 2, 3] # works the same for numpy array
your_seperator = "," # for new line use "\n"

with open("path/to/your/file/your_array.txt", "w") as output_file:
    output_file.write(your_seperator.join(your_array)

Alternatively:

np.savetxt("your_array.csv", your_array, delimiter=",")