views:

94

answers:

2

i have an array called MAC1_Val:

MAC1_Val array([ 1.00000000e+00, -1.00000000e+01, -2.06306600e+02, 2.22635749e+02, 1.00000000e+00, 1.00000000e+01, 1.00000000e+01, -2.06306600e+02, 2.22635749e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00, -1.08892735e+01, 1.88607749e+01, 1.03153300e+01, -1.78666757e+01, 3.33333333e-07, -3.33333333e-07, -4.21637021e-05, 4.21637021e-05, 9.98844400e-01, -1.73973001e-03, 1.20938900e-03, 1.87742948e-03, -3.33333333e-03, 6.66666667e-03, -3.33333333e-03, -2.64911064e-01, -2.60959501e+01, 2.81614422e+01, 3.33333333e-03, -6.66666667e-03, 3.33333333e-03, 0.00000000e+00, 0.00000000e+00])

and i want to write in file (.txt) values in specific format like this:

1.000000e+00
-1.000000e+01 
-2.063066e+02
2.226357e+02
1.000000e+00   
1.000000e+01 .......

note that are 6 digits behind floating point

any suggestions how to do this? thanks in advance!

+1  A: 

Use string interpolation to format the number.

'%.3f' % (1.23456,)
Ignacio Vazquez-Abrams
i tried with it but i didn't get exponent at the end..
sasha
And then just write it (plus a newline) using the vanilla `write` method of the file object.
Noufal Ibrahim
the 'e' format is for esponential, f for float, so it's better to use "%1.6e".
Enrico Carlesso
@sasha: use %e format, not %f.
Tadeusz A. Kadłubowski
>>> '%.3f' % (1467.23456,)'1467.235'and i need 1.467+e03
sasha
@Enrico Carlesso,@Tadeusz A. Kadłubowski :thanks a lot guys!!!!that's exactly what i need!!!!!!
sasha
+2  A: 

printf standard is your friend:

for i in MAC1_Val:
    print "%.6e" % i

1.000000e+00
-1.000000e+01
-2.063066e+02
2.226357e+02
1.000000e+00
1.000000e+01
1.000000e+01
Enrico Carlesso
Or just `"%.6e" % i`. I don't think that the `1` in `"%1.6e"` means what you think it means...
Mark Dickinson
You're right, Mark.
Enrico Carlesso