0

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)
3
  • So your primary goal is to output the data in that format? i.e. print to screen? Or to a file? Have you looked at pythons string formatting yet? What else have you tried? You could iterate over the numpy arrays simultaneously and create lines one at a time for instance. Commented Aug 6, 2014 at 14:13
  • print it to a file just in the same way my input file is, with the res column added Commented Aug 6, 2014 at 14:14
  • 1
    If you want the other terms you can also do 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. Commented Aug 6, 2014 at 19:33

1 Answer 1

2

Rather than put the answer together, I will show you individual parts so you can work through this yourself.

To iterate over multiple numpy arrays simultaneously, you could do the following:

for n, mn, fe, result in zip(N, Mn, Fe, res):
    print "" + str(n) + " " + str(mn) +" " + str(fe) + " " + str(result)

However, to perform the desired formatting, you should use string specifiers: https://docs.python.org/2/library/string.html#format-specification-mini-language

One example would be something like

v = 1000.526
s = "%e   %e\n" % (10.25, v)
print s

writing to a file is as simple as doing:

s = "Here is a line I will write to my file\n"
with open("ec2.dat", 'a') as outfile:
    outfile.write(s)

Linking these things together you should be able to print your desired output to the screen or a file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.