In the example below, tmp.dat corresponds to the output file and X (strings), Y (floats) and Z (floats) are the arrays you want to export.
file=open('tmp.dat','w') #create file
for x,y,z in zip(X,Y,Z):
file.write('%s\t%f\t%f\n'%(x,y,z)) #assume you separate columns by tabs
file.close() #close file
Be sure to modify the %s and %f if you change the array data types.
Thanks to Tyrel Johnson for the trick.
probably a faster way to do that whould be to avoid the for loop
ReplyDeleteimport numpy as np
a = ['a', 'b', 'c']
b = [1, 2, 3]
c = np.array([a,b]).T
np.savetxt('output_file', c, fmt='%s')
The article presents a clear and practical approach to exporting NumPy arrays into a structured ASCII file, making it especially useful for scientific and astronomical data workflows. The simple use of zip() and formatted output provides an easy-to-understand solution, while the discussion also highlights how NumPy’s built-in tools can make the process even more efficient.
ReplyDeletesprayable concrete
kitchen exhaust system cleaning melbourne
ambulance hotline