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?
Asked
Active
Viewed 108 times
1 Answers
1
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=",")

Felix
- 167
- 1
- 8
-
If the Numpy array is 3D, is there a way to keep this shape? Or do I have to do it manually in Comsol? – Ghislain Viguier Oct 07 '22 at 07:50
-
it really depends on what your tool is expecting. you can try the default numpy savetxt function (see my updated answer) – Felix Oct 07 '22 at 07:57