1

I have a nxm numpy array that either has positive values with 8 decimal points like 0.02113342 or NoValue data that is -9999. I am using the line below to make a text file from the numpy array

numpy.savetxt("result.asc", Numpy_Array, fmt="%.8f")#2D array to ASCII

However, I will have -9999.00000000 instead of -9999. I open the file and replace those numbers with -9999 using the following code:

with file("result.asc", 'r') as original: 
    data = original.read()
    new = data.replace(str(-9999)+".00000000", str(-9999))
with file("result.asc", 'w') as modified:
    modified.write(new)

Is there a more elegant way to write -9999 rather than -9999.00000000 from the beginning instead of opening the whole file again and replacing them?

2
  • Why not let the -9999.00000000 persist? Commented Aug 4, 2014 at 15:14
  • The ASCII file will be much bigger and there is no value to it! :) Commented Aug 4, 2014 at 15:17

1 Answer 1

5

Try fmt="%.8g". It is not the same as "%.8f" for all floating point values (e.g. for very small values, it uses exponential notation), but it might work for the cases you have. (See the table at https://docs.python.org/2/library/string.html#format-specification-mini-language with the floating point types--scroll down a bit--for an explanation of the difference between the f and g formats.)

In [188]: x
Out[188]: 
array([[    0.20134635, -9999.        ],
       [    0.9287082 ,     0.00000123],
       [    0.77482316,     0.27246281],
       [    0.40529746,     0.41133371]])

In [189]: np.savetxt("xf.dat", x, fmt="%.8f")

In [190]: np.savetxt("xg.dat", x, fmt="%.8g")

In [191]: !cat xf.dat
0.20134635 -9999.00000000
0.92870820 0.00000123
0.77482316 0.27246281
0.40529746 0.41133371

In [192]: !cat xg.dat
0.20134635 -9999
0.9287082 1.2345679e-06
0.77482316 0.27246281
0.40529746 0.41133371

Here's the actual value of x[1,1]:

In [193]: x[1,1]
Out[193]: 1.23456789e-06

Alternatively, take a look at this answer to a similar question: How to format in numpy savetxt such that zeros are saved only as "0"

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

2 Comments

It totally worked! Perfect! Can you please add an explanation about the differences between fmt="%.8g" and fmt="%.8f" in your answer?
@bikhaab: I added a link to the Python documentation.

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.