I am trying to add an array like this given below
# N Mn Fe x x2
3.94870000e+01 -1.22950000e-07 -1.65130000e-05 6.40000000e-01 0.00000000e+00
3.95040000e+01 -9.38580000e-07 -1.63070000e-05 6.41000000e-01 0.00000000e+00
3.95130000e+01 -1.67100000e-06 -1.59280000e-05 6.42000000e-01 0.00000000e+00
3.95230000e+01 -2.29230000e-06 -1.53800000e-05 6.43000000e-01 0.00000000e+00
The code I have managed to write do add the column Mn and Fe, but not yet managed to write it in column as:
# N Mn Fe Res
The code that I have written is:
#!/usr/bin/env python3
# encoding: utf-8
import numpy as np
inp = "ec.dat"
out = "ec2.dat"
N, Mn, Fe, x, x2 = np.loadtxt(inp, unpack=True)
res = Mn+Fe
print(N, res)
# with open("ec2.dat", 'a') as outfile:
Will anyone kindly help me in writing the table properly? Regards,
EDIT @Paul, Thanks. The complete code is now:
#!/usr/bin/env python3
# encoding: utf-8
import numpy as np
inp = "ec.dat"
out = "ec2.dat"
N, Mn, Fe, x, x2 = np.loadtxt(inp, unpack=True)
res = Mn+Fe
with open("ec2.dat", "w") as of:
for n, mn, fe, res in zip(N, Mn, Fe, res):
s = "%e %e\n" % (n, res)
of.write(s)
s = "%e %e %e %e\n" % (n, mn, fe, res)and the spacing between each "%e" is entirely adjustable if you need to change your formatting.